jQuery计数下一个元素中的复选框

时间:2018-09-23 17:24:34

标签: jquery count next checked

我的示例正在计算页面上的每个复选框。 如何使用jQuery计算下一个/同级元素上的选中复选框?

PS:我希望它可以在单个页面上处理多种表单,而无需调用element(form)ID。

$(document).ready(function() {
  function countChecked() {
    var tCount = $("input:checked").length;
      $(".totalchecked").text( tCount + ' selected');
  }
  countChecked();
  var tCheck = $(".totalchecked");
  $(tCheck).next().find(":checkbox").click(countChecked);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>form 1:
  <div class="totalchecked">0 selected</div>
  <div>
    <input type="checkbox" class="class" name="1">
    <input type="checkbox" class="class" name="2">
    <input type="checkbox" class="class" name="3">
  </div>
</div>
<div>form 2:
  <div class="totalchecked">0 selected</div>
  <div>
    <input type="checkbox" class="class" name="4">
    <input type="checkbox" class="class" name="5">
    <input type="checkbox" class="class" name="6">
  </div>
</div>

/ *更新* /
我想要这样的东西。
enter image description here

3 个答案:

答案 0 :(得分:2)

检查此工作示例:

function checkTotalCheckedBoxes(){
$("div" ).each(function( ) {
  if(this.children.length === 3){ 
  var checked = 0;
	$(this.children).each(function() {
        this.checked ? checked++ : null;      
        $("input[name="+this.name+"]").parent().siblings().closest('.totalchecked')[0].innerText = (checked) + " selected"; 

  	    });
  }
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>form 1:
  <div class="totalchecked">0 selected</div>
  <div>
<input type="checkbox" class="class" name="1"  onclick="checkTotalCheckedBoxes()">
<input type="checkbox" class="class" name="2"  onclick="checkTotalCheckedBoxes()">
<input type="checkbox" class="class" name="3"  onclick="checkTotalCheckedBoxes()">
  </div>
</div>
<div>form 2:
  <div class="totalchecked">0 selected</div>
  <div>
<input type="checkbox" class="class" name="4"  onclick="checkTotalCheckedBoxes()">
<input type="checkbox" class="class" name="5"  onclick="checkTotalCheckedBoxes()">
<input type="checkbox" class="class" name="6"  onclick="checkTotalCheckedBoxes()">
  </div>
</div>

答案 1 :(得分:0)

$('input[type=checkbox]').change(function() {
        var siblingsChecked = [];
           $(this).siblings().each(function(el){
                if($(this).is(':checked')) {
                    siblingsChecked.push(el);
                }
          });
        console.log(siblingsChecked.length);
    });

希望这对您有帮助!

答案 2 :(得分:0)

这是我自己的解决方案,希望对您有所帮助。

$( document ).ready(function() {
    var $SelectCount = $("input:checkbox")
    $SelectCount.change(function(){
        var countChecked = $(this).parent().find("input:checkbox").filter(':checked').length;
        $(this).parent().siblings(".totalchecked").text(countChecked + " Selected");
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>form 1:
  <div class="totalchecked">0 selected</div>
  <div>
    <input type="checkbox" class="class" name="1">
    <input type="checkbox" class="class" name="2">
    <input type="checkbox" class="class" name="3">
  </div>
</div>
<div>form 2:
  <div class="totalchecked">0 selected</div>
  <div>
    <input type="checkbox" class="class" name="4">
    <input type="checkbox" class="class" name="5">
    <input type="checkbox" class="class" name="6">
  </div>
</div>