【问题标题】:Find all unchecked checkboxes in jQuery在 jQuery 中查找所有未选中的复选框
【发布时间】:2012-01-17 22:41:35
【问题描述】:

我有一个复选框列表:

<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" />

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

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

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

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

有人能解释一下这个问题吗? 谢谢!

【问题讨论】:

标签: jquery html input checkbox


【解决方案1】:

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

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

【讨论】:

  • 并且使用 input[type=checkbox] 是最快的。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    也可以用纯js这样实现:

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

    【讨论】:

      【解决方案4】:

      您可以通过扩展 jQuerys 功能来做到这一点。这将缩短您必须为选择器编写的文本量。

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

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

      【讨论】:

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

        这将检索所有未选中的复选框并排除我用来选中|取消选中所有复选框的“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 (,).
        

        【讨论】:

        • 您不应该在答案中包含与 OP 完全无关的内容。他不使用chkAll 框,所以提到它有什么意义。
        【解决方案6】:

        你可以这样使用:

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

        【讨论】:

        • 为什么要重复几年前已经提供的指导?此外,请不要在 SO 上发布纯代码 sn-ps。应解释所有答案。
        【解决方案7】:

        要通过class 选择,您可以这样做:

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

        【讨论】:

        • 提出的问题没有class 属性。这是一个不必要的答案,因为:checkbox:not(:checked) 的建议早于 YEARS 给出。
        【解决方案8】:
        $(".clscss-row").each(function () {
        if ($(this).find(".po-checkbox").not(":checked")) {
                       // enter your code here
                    } });
        

        【讨论】:

        • 欢迎来到 StackOverflow!请为您的代码添加解释,以便其他用户(这可能是他们正在寻找的内容)知道您的代码的作用。
        猜你喜欢
        • 1970-01-01
        • 2016-09-09
        • 1970-01-01
        • 2014-12-08
        • 1970-01-01
        • 2011-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多