如何获得基于特定的类

问题描述:

我已经陷在越来越选中的复选框的复选框的总数,当我点击下面选择如何获得基于特定的类

$("#SelctCheckAll").click(function() { 
     $('input:checkbox').not(this).prop('checked', this.checked); 
     var numberOfChecked = $('input:checkbox:checked').length; 
     var totalCheckboxes = $('input:checkbox').length; 
    }); 

所有的代码所示。这样它的正常工作得到的复选框的总数。但我的疑问是,当我添加两个类,例如class =“checkbox1”和class =“checkbox2”都有type = checkbox。

例如,class = checkbox1包含6个复选框,class = checkbox2包含10个复选框。那么如何获得class = checkbox1的复选框总数,反之亦然?下面

<input type="checkbox" id="SelctCheckAll" name="SelctCheckAll"> 
<input type="checkbox" class="checkbox1"> 
<input type="checkbox" class="checkbox2"> 
+0

'$( '输入:checkbox.checkbox1:检查')长;'和'$('输入:复选框.checkbox2:checked')。length;' – guradio

+0

https://jsfiddle.net/Vinay199129/r4k6z6v0/ – Rex

用样品只需要类的名称添加到您已经在使用这一说法,得到checked复选框:

$('input.checkbox1:checkbox:checked').length 

$("#SelctCheckAll").click(function() { 
 
    $('input:checkbox').not(this).prop('checked', this.checked); 
 
    var numberOfChecked = $('input:checkbox:checked').length; 
 
    var totalCheckboxes = $('input:checkbox').length; 
 

 
    var checkbox1 = $('input.checkbox1:checkbox:checked').length; 
 
    console.log('Total checkbox1: ' + checkbox1); 
 

 
    var checkbox2 = $('input.checkbox2:checkbox:checked').length; 
 
    console.log('Total checkbox2: ' + checkbox2); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="checkbox" id="SelctCheckAll" name="SelctCheckAll"> 
 
<input type="checkbox" class="checkbox1"> 
 
<input type="checkbox" class="checkbox1"> 
 
<input type="checkbox" class="checkbox1"> 
 
<input type="checkbox" class="checkbox1"> 
 
<input type="checkbox" class="checkbox1"> 
 
<input type="checkbox" class="checkbox1"> 
 
<input type="checkbox" class="checkbox1"> 
 

 
<input type="checkbox" class="checkbox2"> 
 
<input type="checkbox" class="checkbox2"> 
 
<input type="checkbox" class="checkbox2"> 
 
<input type="checkbox" class="checkbox2"> 
 
<input type="checkbox" class="checkbox2"> 
 
<input type="checkbox" class="checkbox2"> 
 
<input type="checkbox" class="checkbox2">

+0

非常感谢你,它工作得很好 –

+0

不客气,如果它对你有帮助,请[upvote and accept the answer] (https://stackoverflow.com/help/someone-answers)快乐编码! :) –

你可以得到检查复选框的总数如下:

$(input[type="checkbox"]).is(':checked').length; 

或使用class,如:

$('.class1').is(':checked').length; 

<input type="checkbox" id="SelctCheckAll" name="SelctCheckAll" class="testcheckbox"> 
<input type="checkbox" class="testcheckbox"> 
<input type="checkbox" class="testcheckbox"> 
var count = $('input[type="checkbox"].testcheckbox').length; 
alert(count); 
+0

非常感谢你 –

var checkbox1 = $('input.checkbox1:checkbox:checked').length; //for class checkbox1 
var checkbox2 = $('input.checkbox2:checkbox:checked').length; //for class checkbox2 

你去那里用短小提琴:

我用了jQuery filter()方法来获取所有我需要的信息 - 比如带有某个c的复选框的总数lass,也只有选中的某个类的复选框。

看一看工作小提琴这里:

var elems = $('input[type="checkbox"]'); 
 

 
elems.on('click', function(e){ 
 
\t if(e.target.id === "SelectCheckAll") elems.prop('checked', (elems.prop('checked')) ? true : false); 
 
\t getStatistics(); 
 
}); 
 

 
function getStatistics(){ 
 
\t console.log('Total count of ".checkbox1":' + elems.filter(".checkbox1").length); 
 
    console.log('Total count of ".checkbox2":' + elems.filter(".checkbox2").length); 
 
    console.log('Count of checked ".checkbox1":' + elems.filter(".checkbox1:checked").length); 
 
    console.log('Count of checked ".checkbox2":' + elems.filter(".checkbox2:checked").length); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="checkbox" id="SelectCheckAll" name="SelctCheckAll"> 
 

 
<input type="checkbox" class="checkbox1"> 
 
<input type="checkbox" class="checkbox1"> 
 
<input type="checkbox" class="checkbox1"> 
 
<input type="checkbox" class="checkbox1"> 
 
<input type="checkbox" class="checkbox1"> 
 

 
<input type="checkbox" class="checkbox2"> 
 
<input type="checkbox" class="checkbox2"> 
 
<input type="checkbox" class="checkbox2"> 
 
<input type="checkbox" class="checkbox2">

+1

它工作得很好..非常感谢你 –