使用jQuery单击某个按钮时如何获取ID [英] how to get the id when a certain button is clicked using jquery
本文介绍了使用jQuery单击某个按钮时如何获取ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用jQuery单击#moveToAnTable时,如何获取tbody输入的ID?谢谢.
How can I get the id of tbody's input when #moveToAnTable is clicked using jQuery? Thanks.
<table id="yearSectionAdd2Table">
<thead>
<tr id="columnHeader">
<th>
<div>
<input id="moveToAnTable" type="button" value="<<">
</div>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<center>
<input id="moveToAnTableOne" type="button" value="<<">
</center>
</td>
</tr>
</tbody>
</table>
推荐答案
使用 jQuery.closest 进行操作获取按钮表. 之后,搜索输入.
Use jQuery.closest to get the table of the button. After that search for the input.
jQuery.attr 将返回匹配项中第一个元素的属性:
jQuery.attr will return the attribute of the first element in the match:
$("#moveToAnTable").click(function(){
var id = $(this).closest("table").find("tbody input").attr("id");
alert(id);
});
看小提琴:
这篇关于使用jQuery单击某个按钮时如何获取ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文