简体   繁体   English

我如何将选中复选框的数量添加到此jQuery / JS代码

[英]How can i add the numbers of checked checkbox to this jquery / js code

Please how can i add and display the numbers of checked checkboxes on this line of codes. 请我如何在此代码行上添加和显示已选中复选框的数量。

This is how the checkboxes works: It has a parent and child checkbox; 复选框的工作方式如下:它具有一个父子复选框。 when i check the parent checkbox (Checkall), it checks all (child checkboxes) and displays a div, when i each child checkbox (Single), it displays the div as well, and when i uncheck (Parent/Child) the div hides (jquery hide and show). 当我选中父复选框(Checkall)时,它会选中所有(子复选框)并显示一个div,当我每个子复选框(Single)时,它也会显示div,而当我取消选中(Parent / Child)时,div隐藏(jQuery隐藏和显示)。

So, my problem here is that i want to add checkbox counter to the code. 所以,我的问题是我想在代码中添加复选框计数器。 How to display the number count when i check the parent (Check all) and when i check each child (Single), and my div will still display (hide and show). 当我检查父母(全部选中)和检查每个孩子(单身)时如何显示数字计数,并且我的div仍会显示(隐藏并显示)。

Please, help. 请帮忙。 Thanks a lot in advance. 非常感谢。 You can improve the codes (renovate) or rewrite. 您可以改进代码(更新)或重写。 Here is the code below 这是下面的代码

 $(document).ready(function() { $('#global').click(function() { $('.child').prop('checked', $(this).is(':checked')); if ($(this).is(':checked')) $('#mydiv').show(); else $('#mydiv').hide(); }); $('.child').change(function() { var checkedLength = $('.child:checked').length; if (checkedLength > 0) $('#mydiv').show(); else $('#mydiv').hide(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="mydiv" style="display:none;">RESTORE | DELETE</div> <input type="checkbox" id="global"> <br> <input type="checkbox" class="child"> <input type="checkbox" class="child"> <input type="checkbox" class="child"> <input type="checkbox" class="child"> <input type="checkbox" class="child"> <input type="checkbox" class="child"> <input type="checkbox" class="child"> 

I'll be so glad :) 我会很高兴的:)

This displays the number of child checkboxes that are checked: 这将显示已选中的子复选框的数量:

 $(document).ready(function() { $('#global').click(function() { $('.child').prop('checked', $(this).is(':checked')); if ($(this).is(':checked')) $('#mydiv').show(); else $('#mydiv').hide(); count(); }); $('.child').change(function() { var checkedLength = $('.child:checked').length; if (checkedLength > 0) $('#mydiv').show(); else $('#mydiv').hide(); count(); }); }); var count = function() { var i = $('input.child:checked').length; $('#counter').html(i); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="mydiv" style="display:none;">RESTORE | DELETE <span>Checked: </span> <span id="counter"></span> </div> <input type="checkbox" id="global"> <br> <input type="checkbox" class="child"> <input type="checkbox" class="child"> <input type="checkbox" class="child"> <input type="checkbox" class="child"> <input type="checkbox" class="child"> <input type="checkbox" class="child"> <input type="checkbox" class="child"> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM