在 jquery 中找到所有未选中的复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8465821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:33:49  来源:igfitidea点击:

find all unchecked checkbox in jquery

jqueryhtmlinputcheckbox

提问by Gary

I have a list of checkboxes:

我有一个复选框列表:

<input type="checkbox" name="answer" id="id_1' value="1" />
<input type="checkbox" name="answer" id="id_2' value="2" />
...
<input type="checkbox" name="answer" id="id_n' value="n" />

I can collect all the values of checked checkboxes; my question is how can get all the values of unchecked checkboxes? I tried:

我可以收集所有选中复选框的值;我的问题是如何获取未选中复选框的所有值?我试过:

$("input:unchecked").val();

to get an unchecked checkbox's value, but I got:

获得未选中复选框的值,但我得到:

Syntax error, unrecognized expression: unchecked.

语法错误,无法识别的表达式:未选中。

can anybody shed a light on this issue? thank you!

任何人都可以阐明这个问题吗?谢谢你!

回答by SLaks

As the error message states, jQuery does not include a :uncheckedselector.
Instead, you need to invert the :checkedselector:

正如错误消息所述,jQuery 不包含:unchecked选择器。
相反,您需要反转:checked选择器:

$("input:checkbox:not(:checked)")

回答by dave

$("input:checkbox:not(:checked)")Will get you the unchecked boxes.

$("input:checkbox:not(:checked)")将为您提供未选中的框。

回答by Thulasiram

You can do so by extending jQuerys functionality. This will shorten the amount of text you have to write for the selector.

您可以通过扩展 jQuery 功能来实现。这将缩短您必须为选择器编写的文本量。

$.extend($.expr[':'], {
        unchecked: function (obj) {
            return ((obj.type == 'checkbox' || obj.type == 'radio') && !$(obj).is(':checked'));
        }
    }
);

You can then use $("input:unchecked")to get all checkboxes and radio buttons that are checked.

然后,您可以使用$("input:unchecked")来获取所有选中的复选框和单选按钮。

回答by Pavel Griza

Also it can be achieved with pure js in such a way:

也可以通过纯js​​以这种方式实现:

var matches = document.querySelectorAll('input[type="checkbox"]:not(:checked)');

回答by cdub

$("input[type='checkbox']:not(:checked):not('\#chkAll\')").map(function () { 
   var a = ""; 
   if (this.name != "chkAll") { 
      a = this.name + "|off"; 
   } 
   return a; 
}).get().join();

This will retrieve all unchecked checkboxes and exclude the "chkAll" checkbox that I use to check|uncheck all checkboxes. Since I want to know what value I'm passing to the database I set these to off, since the checkboxes give me a value of on.

这将检索所有未选中的复选框并排除我用来选中|取消选中所有复选框的“chkAll”复选框。因为我想知道我传递给数据库的值是什么,所以我将它们设置为关闭,因为复选框给了我一个打开的值。

//looking for unchecked checkboxes, but don't include the checkbox all that checks or unchecks all checkboxes
//.map - Pass each element in the current matched set through a function, producing a new jQuery object containing the return values.

//.get - Retrieve the DOM elements matched by the jQuery object.
//.join - (javascript) joins the elements of an array into a string, and returns the string.The elements will be separated by a specified separator. The default separator is comma (,).

回答by Ehsan

You can use like this :

你可以这样使用:

$(":checkbox:not(:checked)")

回答by Chukwuemeka Inya

To select by class, you can do this:

要选择 by class,您可以执行以下操作:

$("input.className:checkbox:not(:checked)")

回答by UTHIRASAMY

$(".clscss-row").each(function () {
if ($(this).find(".po-checkbox").not(":checked")) {
               // enter your code here
            } });