admin管理员组文章数量:1334826
I have the following input on my page:
<input type="text" name="r1" />
I am trying to select it with JQuery, using $("[name='r1']")
, and for some reason it returns null
. Ok, fine, the title is misleading in that it is me that is doing something wrong; can you tell me what?
EDIT:
$('input[name="r1"]')
doesn't work either. Also, sorry for the typo.
I have the following input on my page:
<input type="text" name="r1" />
I am trying to select it with JQuery, using $("[name='r1']")
, and for some reason it returns null
. Ok, fine, the title is misleading in that it is me that is doing something wrong; can you tell me what?
EDIT:
$('input[name="r1"]')
doesn't work either. Also, sorry for the typo.
- 3 Are you referencing it after the element is rendered? – epascarello Commented Oct 25, 2012 at 12:49
- the name in your example is "r1" not "r2" is that at typo here or is there an "r2" in your code you are trying to select? – ntlarson Commented Oct 25, 2012 at 12:51
- - check working demo on fiddle by clicking link in my answer ...its working properly... – Pranay Rana Commented Oct 25, 2012 at 13:10
- If the selectors don't seem to work it's probably because your are attempting to find them before the page has actually (fully) loaded. Make sure your jQuery code to find elements is running after the document has loaded $(document).ready(function() {... } – Matthew Lock Commented Aug 8, 2016 at 7:34
7 Answers
Reset to default 7Last EDIT
Working DEMO
Html Code
<input type="text" name="r1" />
Jquery Code
$(document).ready(function() {
alert($('input[name="r1"]').length);
});
OLD Updates
you have typo error here you are trying to find r2
element which is not there just update code like $("[name='r1']")
or
to find it proper way you should do as below
$("input[name='r2']")
instead of $("[name='r2']")
only
EDIT
According to you edited question you should check example over here : Attribute Equals Selector [name="value"]
EDIT
for proper check you can do like this
$(document).ready(function() {
alert($('input[name="r1"]').length);
});
Just made a quick test:
<input type="text" name="r1" />
with the following Javascript
$('input[name=r1]').click(function(e){
alert('found');
});
It worked perfectly!
You are not telling jQuery what element you're looking for, that is you input
Try doing it this way
$('input[name=r1]')
I think that the selector is wrong. Should be r1
as opposed to r2
.
jsFiddle
$("[name=r1]")
you need to include the html tag for attribute filters.
$("input[name='r2']");
Please refer to this link for further information on attribute selectors.
It will work definetly:
$("*[name='r1']")
You can use both $("[name='r1']")
and $('input[name="r1"]')
in this scenario
Use $("[name='r1']")
if you want to select any element with attribute called "name" equal to "r1"
Use $('input[name="r1"]')
if you want to select only input elements with attribute called "name" equal to "r1"
More JQuery Attribute fillter examples are listed here
本文标签: javascriptJQuery selectors don39t workStack Overflow
版权声明:本文标题:javascript - JQuery selectors don't work - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742265578a2443326.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论