javascript - jQuery : Counting checked checkboxes - wrong count -
i have code counts checked checkboxes. works fine when selecting, count breaks (adds +1 count) when deselect of selected checkboxes.
html:
<div>number of checkboxes checked: <span id='chck'>0</span></div> <table> <tr><td><input type='checkbox' /></td></tr> <tr><td><input type='checkbox' /></td></tr> <tr><td><input type='checkbox' /></td></tr> <tr><td><input type='checkbox' /></td></tr> <tr><td><input type='checkbox' /></td></tr> <tr><td><input type='checkbox' /></td></tr> <tr><td><input type='checkbox' /></td></tr> </table> js:
$('tr :checkbox').change(function(){ $('#chck').text($(this).add(':checked').length); }); fiddle: http://jsfiddle.net/kasxj/
not sure why you're using $(this).add(':checked') when $(':checked') suffice.
Comments
Post a Comment