views:

3052

answers:

4

Suppose I have the following 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[]'?

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

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

+9  A: 

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

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

Ideally, you'd be able to do this:

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

But as this answer explains, jQuery uses this to parse the value part of the attr=value condition:

(['"]*)(.*?)\3|)\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.

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.

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

Paolo Bergantino
I believe, that this is the bug and it's related fix: http://dev.jquery.com/ticket/3443
altCognito
It's not fixed in jQuery 1.3.2, which appears to be the latest generally available version.
bobince
It is fixed in my 1.3.2....
Paolo Bergantino
A: 

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");
Tacoman667
However, the [] may be worng :)
Tacoman667
#xyz is for ‘id’ attributes, not ‘name’.
bobince
+1  A: 

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

$("#myform input[name='foo[]']:checked:enabled");
Seb
This won't work, unfortunately. The jQuery parser ends at the first ] it encounters, so [] aren't allowed in this case.
Paolo Bergantino
Indeed. This is a bug; see http://stackoverflow.com/questions/739695 for discussion.
bobince
An old bug. It's fixed if you're using the latest version appraently, as you can see my example shows.
altCognito
My comment there was about the latest version (at the time of writing: 1.3.2). The regex used to parse attr selectors is pretty much totally broken in this version. Maybe a fix will be forthcoming later.
bobince
+1  A: 

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

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.

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

altCognito
*mumble* *mumble* *mumble* If I'm wrong, I'll recant, but the example is there.
altCognito
Touche, kind sir. -1 removed, +1. I've updated my answer with this newfound knowledge.
Paolo Bergantino
Sorry for the confusion, I was going by what bobince had said in an answer a few days ago so I figured it was a current thing. Will check for myself next time.
Paolo Bergantino
Oh, a good point. This question conversation has gotten pretty convoluted.
altCognito
added it for you, hope you don't mind
Russ Cam
No problem at all. Thank you!
altCognito