-2 javascript jquery html-table
我正在尝试获取动态生成表的id属性.所以,如果我点击第一个链接,我想得到"editEmploee-4".
<table id="example" class="table span12 table-bordered table-hover">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<tr>
<td>Person 1</td>
<td>Name Person 1</td>
<td><a href="#" class = "editDialog" id="editEmployee-4"><img src="img/edit.png" height="20" /></a></td>
</tr>
<tr>
<td>Person 2</td>
<td>Name Person 2</td>
<td><a href="#" class = "editDialog" id="editEmployee-5"><img src="img/edit.png" height="20" /></a></td>
</tr>
<tr>
<td>Person 3</td>
<td>Name Person 3</td>
<td><a href="#" class = "editDialog" id="editEmployee-47"><img src="img/edit.png" height="20" /></a></td>
</tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
我已经尝试过这段代码,但我总是得到相同的ID.
var data = $( "a[id][class=editDialog]" ).attr('id');
Run Code Online (Sandbox Code Playgroud)
你知道如何获得动态生成的链接ID吗?
活动代表团:
$(document).on("click", "#example a", function() {
console.log(this.id); //id of clicked link
});
Run Code Online (Sandbox Code Playgroud)
不确定动态生成了多少表,但是您通常希望使用容器元素而不是 document
$(".editDialog").click(function(e)
{
e.preventDefault();
var myID = $(this).attr("id");
// do something with myID
})
Run Code Online (Sandbox Code Playgroud)