How to find all unselected checkboxes?

2019-04-04 19:22发布

问题:

In jQuery, how does one go about finding all the 'unchecked' checked boxes.

$(':checkbox:checked');

appears to be me all checked boxes, but what I need is all non-checked boxes.

回答1:

You use the :not selector, like so:

$('input:checkbox:not(:checked)');

Or the .not function, like so:

$('input:checkbox').not(':checked');

Also note that you should always put input before filters like :radio and :checkbox, as without that the selector are evaluated as *:checkbox which is a really slow selector.



回答2:

A solution without special jQuery selectors, using the attribute selector [docs] and .filter() [docs]:

$('input[type="checkbox"]').filter(function() {
    return !this.checked;
});