jQuery:选择选中的复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/747670/
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-26 09:42:40  来源:igfitidea点击:

jQuery: selecting checked checkbox

jquerycheckboxinputjquery-selectors

提问by bart

Suppose I have the following HTML:

假设我有以下 HTML:

<form id="myform">
  <input type='checkbox' name='foo[]'/> Check 1<br/>
  <input type='checkbox' name='foo[]' checked='true'/> Check 2<br/>
  <input type='checkbox' name='foo[]'/> Check 3<br/>
</form>

Now, how do I select the checked input fields with name 'foo[]'?

现在,如何选择名称为“foo[]”的选中输入字段?

This is my attempt, but it doesn't work:

这是我的尝试,但不起作用:

$("#myform input[name='foo']:checked:enabled");

回答by Paolo Bergantino

The name of the field isn't foo, it is foo[]. You could use the attributeStartsWithselector:

该字段的名称不是foo,而是foo[]。您可以使用attributeStartsWith选择器:

$("input[name^='foo']:checked:enabled",'#myform');

Ideally, you'd be able to do this:

理想情况下,您可以这样做:

$("input[name='foo[]']:checked:enabled",'#myform');

But as this answerexplains, jQuery uses this to parse the valuepart of the attr=valuecondition:

但是正如这个答案所解释的那样,jQuery 使用它来解析条件的value一部分attr=value

(['"]*)(.*?)|)\s*\]

\3 being the group containing the opening quotes, which weirdly are allowed to be multiple opening quotes, or no opening quotes at all. The .*? then can parse any character, including quotes until it hits the first ‘]' character, ending the match. There is no provision for backslash-escaping CSS special characters, so you can't match an arbitrary string value in jQuery.

\3 是包含开头引号的组,奇怪的是允许是多个开头引号,或者根本没有开头引号。这 。*?然后可以解析任何字符,包括引号,直到它遇到第一个 ']' 字符,结束匹配。没有提供反斜杠转义 CSS 特殊字符的规定,因此您无法匹配 jQuery 中的任意字符串值。

In other words, as soon as jQuery hits the first ]it thinks the value is over. So you are stuck with startsWith or using pure DOM elements, as that answer also explains.

换句话说,一旦 jQuery 命中第一个,]它就会认为该值已经结束。所以你被困在 startsWith 或使用纯 DOM 元素,正如那个答案也解释的那样。

SUPER DUPER IMPORTANT EDIT:
This bug is fixed, apparently. You should be able to use the code I described as "ideal", above.

SUPER DUPER 重要编辑
显然,此错误已修复。您应该能够使用我在上面描述为“理想”的代码。

回答by Seb

You should quote the attribute when selecting and include []:

您应该在选择和包含时引用该属性[]

$("#myform input[name='foo[]']:checked:enabled");

回答by cgp

Actually, what you have works just fine, just use double quotes, and include the brackets so it can match.

实际上,您所拥有的效果很好,只需使用双引号,并包含括号以便匹配。

 $("#myform input[name='foo[]']:checked:enabled");

You can see it in action here: http://jsbin.com/oyoro

你可以在这里看到它的实际效果:http: //jsbin.com/oyoro

Note that jQuery has had issues on and off with brackets in the past, which can explain a lot of the confusion surrounding the need for escaping. I know I've had issue myself with brackets and just quoting.

请注意,jQuery 过去在括号中存在问题,这可以解释围绕转义需要的许多混乱。我知道我在括号中遇到了问题,只是引用了。

See: http://dev.jquery.com/ticket/3443

见:http: //dev.jquery.com/ticket/3443

回答by Tacoman667

I believe it goes something like this:

我相信它是这样的:

$("input #foo[] :checked");

EDIT: The commenter is right. I should have gone with my first response. :) This time I am going to use the "^".

编辑:评论者是对的。我应该接受我的第一反应。:) 这次我要使用“^”。

$("input[name^=foo] :checked");