简体   繁体   English

使用jQuery选择特定标签时,如何更改标签文本?

[英]How do I change a label text when that particular label is selected using jQuery?

I want to update every single label I generate by using the #updatelabel input only. 我只想使用#updatelabel输入来更新生成的每个标签。 Let's say, I generate 3 textboxes along with labels (with incremental id) and I just would like to change the label #2 then how would I do that? 假设我生成了3个带有标签(带有增量ID)的文本框,而我只想更改标签#2,那我该怎么做? Because if I choose to update label #2 then the only one that updates is label #1 and after that I can't update any other label. 因为如果我选择更新标签#2,那么唯一更新的标签就是标签#1,此后,我将无法更新任何其他标签。

Here is a demo http://jsfiddle.net/H9mKZ/ 这是一个演示http://jsfiddle.net/H9mKZ/

I really need a good answer. 我真的需要一个好的答案。 Thanks. 谢谢。

Only one element is allowed to have an ID at a time. 一次只允许一个元素具有ID。 Instead of $("#updatelabel") you should use a class $(".updatelabel") . 而不是$("#updatelabel")您应该使用类$(".updatelabel")

While tandu is correct about the ID property, you can always store a reference to the text box that was clicked and just you that to get the value. 虽然tandu关于ID属性是正确的,但您始终可以存储对单击的文本框的引用,也可以仅存储引用以获取值。 Here is an updated example: http://jsfiddle.net/H9mKZ/1/ 这是一个更新的示例: http : //jsfiddle.net/H9mKZ/1/

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

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