jQuery selector bug? composed selector vs. simple selector & find()

旧街凉风 提交于 2019-12-12 09:57:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!