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.

Share edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Oct 25, 2012 at 12:49 BluefireBluefire 14.1k24 gold badges79 silver badges121 bronze badges 4
  • 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
Add a ment  | 

7 Answers 7

Reset to default 7

Last 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