简体   繁体   English

JQuery选择器不起作用

[英]JQuery selectors don't work

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 . 我试图用JQuery选择它,使用$("[name='r1']") ,并且由于某种原因它返回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. $('input[name="r1"]')也不起作用。 Also, sorry for the typo. 另外,抱歉打字错误。

Last EDIT 最后编辑

Working DEMO 工作演示

Html Code Html代码

<input type="text" name="r1" />​

Jquery Code Jquery代码

$(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']") 你有拼写错误在这里,你试图找到r2元素,这不只是更新代码,如$("[name='r1']")

or 要么

to find it proper way you should do as below 找到你应该做的正确方法如下

$("input[name='r2']") instead of $("[name='r2']") only $("input[name='r2']")而不是$("[name='r2']")

EDIT 编辑

According to you edited question you should check example over here : Attribute Equals Selector [name="value"] 根据您编辑的问题,您应该检查此处的示例: 属性等于选择器[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 使用以下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 你没有告诉jQuery你正在寻找什么元素,那就是你input

Try doing it this way 尝试这样做

$('input[name=r1]')

I think that the selector is wrong. 我认为选择器是错误的。 Should be r1 as opposed to r2 . 应该是r1而不是r2

jsFiddle 的jsfiddle

$("[name=r1]")

you need to include the html tag for attribute filters. 您需要包含属性过滤器的html标记。

$("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 您可以在此场景中同时使用$("[name='r1']")$('input[name="r1"]')

Use $("[name='r1']") if you want to select any element with attribute called " name " equal to " r1 " 如果要选择名为“ name ”的属性等于“ r1 ”的任何元素,请使用$("[name='r1']")

Use $('input[name="r1"]') if you want to select only input elements with attribute called " name " equal to " r1 " 如果要仅选择名为“ name ”的属性等于“ r1 ”的输入元素,请使用$('input[name="r1"]')

More JQuery Attribute fillter examples are listed here 此处列出更多JQuery Attribute fillter示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM