确定是否jQuery对象中的所有复选框被选中,返回布尔 [英] Determine if all checkboxes within jQuery object are checked, return as boolean

查看:131
本文介绍了确定是否jQuery对象中的所有复选框被选中,返回布尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有。我一直在努力,现在摸不着头脑了一段时间。

Hey all. I've been been trying to figure this out for a while now.

创建一个复选框jQuery对象,并将其存储在一个变量:

I create a jQuery object of checkboxes and store it in a variable:

$group1 = $('#checkbox1,#checkbox2,#checkbox3,#checkbox4');

用户无法继续,除非该组中的所有复选框被选中。

The user cannot continue unless all checkboxes in that group are checked.

如果语句。是结合我一直使用的是(':检查')来找到一个布尔值:

I've been using an if statement combined with .is(':checked') to find a boolean value:

if( $group1.is(':checked') ){
  //continue is OK
}

...但。是(':检查')将返回 TRUE 如果的的复选框被集团内进行检查。从本质上讲,。是(':检查')在$ 1组选定的元素执行OR操作。我在寻找一个与操作,因此所有选中的元素必须检查返回 TRUE 。是否有一个jQuery函数,这样做,或另一种解决方法吗?

...but .is(':checked') will return TRUE if any checkboxes are checked within the group. Essentially, .is(':checked') performs an OR operation on the selected elements in $group1. I'm looking for an AND operation, so all selected elements must be checked to return TRUE. Is there a jQuery function that does this, or another workaround?

推荐答案

@Adam是关闭的只是有点

@Adam is off just a bit

if( $group1.filter(':not(:checked)').length === 0){
  //continue is OK
}

这篇关于确定是否jQuery对象中的所有复选框被选中,返回布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆