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.
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]')
you need to include the html tag for attribute filters.
$("input[name='r2']");
Please refer to this link for further information on attribute selectors.
它肯定会起作用:
$("*[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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.