查找所有复选框都在jQuery中检查

时间:2022-04-06 19:37:46

I have a collection of checkboxes

我有一组复选框

 <input id="1" class="paid" type="checkbox" />
 <input id="2" class="paid" type="checkbox" />
 <input id="3" class="paid" type="checkbox" />
 <input id="4" class="paid" type="checkbox" />

I would like to write some jQuery to check if all checkboxes are checked then perform an action but how?

我想写一些jQuery来检查是否所有的复选框都被选中然后执行一个动作,但是如何?

2 个解决方案

#1


60  

Like this:

喜欢这个:

if (!$('input.paid[type=checkbox]:not(:checked)').length)
    do('stuff');

This will check if there are any that are unchecked, and do stuff if there aren't (i.e. they are all checked).

这将检查是否有任何未经检查的东西,如果没有,则进行填充(即,它们都被检查)。

#2


2  

I think there would be a good feature in jquery an are function:

我认为在jquery中有一个很好的功能是一个函数:

jQuery.fn.are = function(selector) {
return !!selector && this.filter(selector).length == this.length;
};

Usage:

用法:

if($('input.paid[type=checkbox]').are(':checked'))

Example:

例:

http://jsfiddle.net/9s2vA/

http://jsfiddle.net/9s2vA/

I have found this function at http://api.jquery.com/is/ written by Tgr , when I was checking if this exists.

我在http://gri.jquery.com/is/找到了这个函数,当时我正在检查是否存在。

#1


60  

Like this:

喜欢这个:

if (!$('input.paid[type=checkbox]:not(:checked)').length)
    do('stuff');

This will check if there are any that are unchecked, and do stuff if there aren't (i.e. they are all checked).

这将检查是否有任何未经检查的东西,如果没有,则进行填充(即,它们都被检查)。

#2


2  

I think there would be a good feature in jquery an are function:

我认为在jquery中有一个很好的功能是一个函数:

jQuery.fn.are = function(selector) {
return !!selector && this.filter(selector).length == this.length;
};

Usage:

用法:

if($('input.paid[type=checkbox]').are(':checked'))

Example:

例:

http://jsfiddle.net/9s2vA/

http://jsfiddle.net/9s2vA/

I have found this function at http://api.jquery.com/is/ written by Tgr , when I was checking if this exists.

我在http://gri.jquery.com/is/找到了这个函数,当时我正在检查是否存在。