Search and count the number of marked mailboxes using jQuery

I have one form per page, and inside this form there will be a check box for each row in the table. I need to count the number of lines that have a check string, but I am having problems even choosing from jQuery.

Here's what my checkbox code looks like:

<input type="checkbox" id="onHomePage_56" name="onHomePage" 
    value="56" checked="checked">

Uncontrolled checkbox not checked = "checked".

+3
source share
4 answers
var q = $('form#id-of-form').find('input:checked').length;

http://api.jquery.com/checked-selector/

If there are flags in the form that you don’t want to count, add a class to the ones you want to count and select them instead:

var q = $('form#id-of-form').find('input.count-these:checked').length;
+7
source

jQuery provides special selectors for this:

$('input:checked').length

More specific:

var num = $('#myform input:checkbox:checked').length;

jQuery :checkbox, :checked . , :checkbox , [type=checkbox], :

var num = $('#myform input[type=checkbox]:checked').length;
+2

You can try the following:

$('#form input:checkbox:checked').length;
+1
source
$('#myForm table tr td input:checked')​.size();​

Demo.

Update: It’s better to filter type, because you can also check the radio buttons.

$('#myForm table tr td input[type="checkbox"]:checked').size();

Demo with checked radio buttons

-1
source

All Articles