问题
Something is very awkward about my situation... i have something like this:
<div id="selector">
<input type='radio' />
<input type='radio' />
<input type='radio' />
</div>
if I use $("#selector input[type=radio]")
all three elements are found, but if I use $("#selector").find("input[type=radio]")
or even find("input")
only the first one is found.
Is this a bug in jQuery? Am I not using find()
properly?
Clarification : I want to use find() to get all the inputs, but anything I try finds only the first one.
edit: i'm using jquery 1.3.2
回答1:
What you really want is:
$("#selector > :radio")
As for why you're getting only one, I'd need to see the actual code that's being run because find() doesn't stop at one and will find all matches so it may be how you're using it afterwards that is the issue.
回答2:
The two code fragments should return the same result.
Are you saying that when you run the following code, the first alert will show "3" and the second "1" ?
var a = $("#selector input[type=radio]");
var b = $("#selector").find("input[type=radio]");
alert(a.length);
alert(b.length);
Can you please verify?
回答3:
Try
$("#selector").find("input[type=radio]")
回答4:
See here
All the three returns the same result!
$(function() {
console.log($("#selector input[type=radio]")); // 3
console.log($("#selector").find("input[type=radio]")); // 3
console.log($("#selector").find("input")); // 3
});
回答5:
$("#selector").children("input[@type=radio]")
来源:https://stackoverflow.com/questions/969647/jquery-selector-bug-composed-selector-vs-simple-selector-find