如何获取选中的复选框的个数?

<input type="checkbox"> 20
<input type="checkbox"> men 20
<input type="checkbox"> men 30
<input type="checkbox"> 20
<input type="checkbox">
<input type="checkbox"> 33333

<button>aa</button> <button>bb</button>

var box = $('tbody input[type=checkbox]');
            var    size = box.length;//取总个数  
                    
            var    chked = box.filter(':checked').length;//取选中的个数
    
      $('button:contains(aa)').click(function(){
     
          console.log(chked);
          console.log(size);
      
      });

为什么输出的选中的个数为0?

阅读 5.1k
3 个回答
var box = $('tbody input[type=checkbox]');
var size = box.length;//取总个数  

$('button:contains(aa)').click(function () {
    var chked = box.filter(':checked').length;//取选中的个数
    
    console.log(chked);
    console.log(size);

});

你要把获取个数的代码也放进click中。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题