简体   繁体   English

jQuery ID 选择器仅适用于第一个元素

[英]jQuery ID selector works only for the first element

I have 3 buttons with same ID.我有 3 个具有相同 ID 的按钮。 I need to get each button's value when it's being clicked.我需要在单击时获取每个按钮的值。

<button id="xyz" type="button" class="btn btn-primary" value="1">XYZ1</button>
<button id="xyz" type="button" class="btn btn-primary" value="2">XYZ2</button>
<button id="xyz" type="button" class="btn btn-primary" value="3">XYZ3</button>

Here is my current jQuery script:这是我当前的jQuery脚本:

$("#xyz").click(function(){
      var xyz = $(this).val();
      alert(xyz);
});

But it works only for the first button, clicking on the other buttons are being ignored.但它只适用于第一个按钮,点击其他按钮被忽略。

I have 3 buttons with same id ...我有 3 个具有相同 ID 的按钮...

You have invalid HTML.您的 HTML 无效。 You can't have more than one element in a page with the same id attribute value.一个页面中不能有多个具有相同id属性值的元素。

Quoting the spec :引用规范

7.5.2 Element identifiers: the id and class attributes 7.5.2元素标识符:id 和 class 属性

id = name [CS] id = 名称 [CS]
This attribute assigns a name to an element.此属性为元素分配名称。 This name must be unique in a document.此名称在文档中必须是唯一的。

Solution : change from id to class :解决方案:从id更改为class

<button type="button" class="btn btn-primary xyz" value="1">XYZ1</button>
<button type="button" class="btn btn-primary xyz" value="2">XYZ2</button>
<button type="button" class="btn btn-primary xyz" value="3">XYZ3</button>

And the jQuery code :jQuery 代码

$(".xyz").click(function(){
    alert(this.value);
    // No need for jQuery :$(this).val() to get the value of the input.
});

But it works only for the first button但它只适用于第一个按钮

jQuery #id selector docs : jQuery #id选择器文档

Each id value must be used only once within a document.每个 id 值在一个文档中只能使用一次。 If more than one element has been assigned the same ID, queries that use that ID will only select the first matched element in the DOM.如果多个元素被分配了相同的 ID,则使用该 ID 的查询将只选择 DOM 中第一个匹配的元素。 This behavior should not be relied on, however;但是,不应依赖这种行为; a document with more than one element using the same ID is invalid.具有多个元素使用相同 ID 的文档是无效的。

If you look at the jQuery source you can see when you call $ with an id selecor-( $("#id") ), jQuery calls the native javascript document.getElementById function:如果您查看 jQuery 源代码,您可以看到当您使用 id selecor-( $("#id") ) 调用$时,jQuery 调用了原生的 javascript document.getElementById函数:

// HANDLE: $("#id")
} else {
    elem = document.getElementById( match[2] );
}

Though, in the spec of document.getElementById they didn't mention it must return the first value, this is how most of (maybe all?) the browsers implemented it.虽然,在document.getElementById规范中,他们没有提到它必须返回第一个值,这就是大多数(也许是全部?)浏览器实现它的方式。

DEMO演示

ID means "Identifier" and is valid only once per document. ID 的意思是“标识符”,每个文件只能使用一次。 Since your HTML is wrong at this point, some browsers pick the first, some the last occuring element with that ID.由于此时您的 HTML 是错误的,因此某些浏览器会选择第一个出现的具有该 ID 的元素,有些则选择最后出现的元素。

Change ids for names would be a good step.更改名称的 id 将是一个很好的步骤。

Then use $('button[name="xyz"]').click(function(){然后使用$('button[name="xyz"]').click(function(){

From my experience, if you use $('button#xyz') selector instead it will work.根据我的经验,如果您使用$('button#xyz')选择器,它将起作用。 That's a hack, but it's still invalid HTML.这是一个 hack,但它仍然是无效的 HTML。

this also worked if you have multiple element with same id.如果您有多个具有相同 ID 的元素,这也有效。

 $("button#xyz").click(function(){
  var xyz = $(this).val();
  alert(xyz);
 });

you can check HERE你可以在这里查看

If you have same id in a container you can use on() to access each element for every event如果容器中有相同的 id,则可以使用 on() 访问每个事件的每个元素

 $("#containers").on("click","#xyz",function(){ alert($(this).val()) })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="containers"> <button id="xyz" type="button" class="btn btn-primary" value="1">XYZ1</button> <button id="xyz" type="button" class="btn btn-primary" value="2">XYZ2</button> <button id="xyz" type="button" class="btn btn-primary" value="3">XYZ3</button> </div>

and info about on() is here关于 on() 的信息在这里

You can't have the same id because id is unique in page HTML.您不能拥有相同的 id,因为 id 在页面 HTML 中是唯一的。 Change it to class or other attribute name.将其更改为类或其他属性名称。

$('attributename').click(function(){ alert($(this).attr(attributename))});

Although changing the id's to a class is better, you can get all the elements with the same id using the attribute equals selector :尽管将 id 更改为类更好,但您可以使用属性 equals 选择器获取具有相同 id 的所有元素:

$('[id="xyz"]')

Or this to get only buttons with id xyz:或者这样只获取带有 id xyz 的按钮:

$('button[id="xyz"]')

Or divs with id xyz:或者 id 为 xyz 的 div:

$('div[id="xyz"]')

etc.等等。

Alternatively you could use the "Attribute Contains Selector" to get all elements with ids that contain "xyz":或者,您可以使用“属性包含选择器”来获取 ID 包含“xyz”的所有元素:

$('[id*="xyz"]')

Of course, this means all elements with id that partially contain "xyz" will get selected by this.当然,这意味着所有 id 部分包含“xyz”的元素都将被选中。

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

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