简体   繁体   English

计算从生成的复选框中检查了多少

[英]Count how many are checked from generated checkbox

I'm working on a simple table that generates number of checkbox, and count the total of how many are checked. 我正在处理一个简单的表,该表生成复选框的数量,并计算被选中的总数。

when I unchecked, it will show how many are the remaining checked checkbox. 当我取消选中时,它将显示剩余的选中复选框有多少。

my problem is when I generate new rows the counter doesn't work anymore. 我的问题是,当我生成新行时,计数器不再起作用。

 $('.items').keyup(function() { var items = $(this).val(); $('span').text(items); $('table tbody tr').remove(); for (var i = 1; i <= items; i++) { $('table > tbody').append('<tr><td><input type="checkbox" checked></td></tr>'); } }); $('input:checkbox').click(function() { var count = $(this).closest('table').find('input:checkbox:checked').length; $('span').text(count); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label for=""> Items </label> <input type="text" class="items"> <span>4</span> <table> <tbody> <tr> <td> <input type="checkbox" checked> </td> </tr> <tr> <td> <input type="checkbox" checked> </td> </tr> <tr> <td> <input type="checkbox" checked> </td> </tr> <tr> <td> <input type="checkbox" checked> </td> </tr> </tbody> </table> 

Use event delegation instead, so that you only need to apply a single listener to the container, otherwise you'll have to re-add listeners to each element every time: 请改用事件委托,这样您只需要将单个侦听器应用于容器,否则,您每次都必须为每个元素重新添加侦听器:

 $('.items').keyup(function() { var items = $(this).val(); $('span').text(items); $('table tbody tr').remove(); for (var i = 1; i <= items; i++) { $('table > tbody').append('<tr><td><input type="checkbox" checked></td></tr>'); } }); $('table').on('click', 'input', function() { var count = $(this).closest('table').find('input:checkbox:checked').length; $('span').text(count); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label for=""> Items </label> <input type="text" class="items"> <span>4</span> <table> <tbody> <tr> <td> <input type="checkbox" checked> </td> </tr> <tr> <td> <input type="checkbox" checked> </td> </tr> <tr> <td> <input type="checkbox" checked> </td> </tr> <tr> <td> <input type="checkbox" checked> </td> </tr> </tbody> </table> 

 $('.items').keyup(function() { var items = $(this).val(); $('span').text(items); $('table tbody tr').remove(); for (var i = 1; i <= items; i++) { $('table > tbody').append('<tr><td><input class="checkeboxclass" type="checkbox" checked></td></tr>'); } }); $('input[type="checkbox"]').on( "change", function() { var count = $('input[type="checkbox"]:checked').length; $('span').text(count); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label for=""> Items </label> <input type="text" class="items"> <span>4</span> <table> <tbody> <tr> <td> <input type="checkbox" checked> </td> </tr> <tr> <td> <input type="checkbox" checked> </td> </tr> <tr> <td> <input type="checkbox" checked> </td> </tr> <tr> <td> <input type="checkbox" checked> </td> </tr> </tbody> </table> 

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

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