如何使用Jquery获取按钮的id值? [英] How to get the id value of a button using Jquery?
问题描述
我有一个动态创建编辑按钮的表格。
按钮的ID是一个附加了 table content id 的字符串。
即: < input id ='edit'+ id type ='button'value ='编辑'onclick = edit(this.id)/>
谢谢。
如何获得使用jquery的按钮的ID(= edit + id) =h2_lin>解决方案
您必须获取输入元素的引用。然后,只需调用
reference.attr('id');
如果它有一个类,例如inputClass,您可以这样做:
var reference = $(。inputClass);
以下是我将如何处理您的特定情况。您有:
< input id ='edit1'type ='button'value ='编辑'onclick = edit(this .id)/>
< input id ='edit2'type ='button'value ='编辑'onclick = edit(this.id)/>
< input id ='edit3'type ='button'value ='编辑'onclick = edit(this.id)/>
我会将其替换为:
< input id ='edit1'type ='button'value ='Edit'class ='inputClass'/>
< input id ='edit2'type ='button'value ='Edit'class ='inputClass'/>
< input id ='edit3'type ='button'value ='Edit'class ='inputClass'/>
然后,在页面的任何位置写下这段代码:
$(document).ready(function(){
$('。inputClass')。each(function(){
$(this).click(function(){
var id = $(this).attr('id');
//执行编辑函数对id
所做的任何操作});
});
});
这为每个调用编辑函数的元素绑定一个函数...
I have a table with dynamically created Edit buttons. The ID of the buttons is a string that is appended with table content id.
i.e: <input id='edit'+id type='button' value='Edit' onclick=edit(this.id) />
How can i get the ID(=edit+id) value of the button using jquery.
Thanks.
You have to get a reference for the input element. Then, just call
reference.attr('id');
If it had a class, for example, inputClass, you could do:
var reference = $(".inputClass");
Here's how I would approach your particular situation. You have:
<input id='edit1' type='button' value='Edit' onclick=edit(this.id) />
<input id='edit2' type='button' value='Edit' onclick=edit(this.id) />
<input id='edit3' type='button' value='Edit' onclick=edit(this.id) />
I would replace this for:
<input id='edit1' type='button' value='Edit' class='inputClass'/>
<input id='edit2' type='button' value='Edit' class='inputClass'/>
<input id='edit3' type='button' value='Edit' class='inputClass'/>
Then, anywhere in your page, you write this piece of code:
$(document).ready(function() {
$('.inputClass').each(function() {
$(this).click(function(){
var id = $(this).attr('id');
//Do whatever the edit function should do with the id
});
});
});
this binds a function for each of the elements which call your edit function...
这篇关于如何使用Jquery获取按钮的id值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!