jQuery查找动态生成的tr标记的ID [英] jquery Find ID of dynamically generated tr tag
问题描述
<tr id = "<?php echo $count; ?>" >
<td>name </td>
<td>info </td>
...
...
<td><a href="delete.php">delete</a></td>
</tr>
我想做的是,当我单击Delete链接时,使用ajax go删除页面,然后做不需要的事情,然后在不刷新页面的情况下删除该行.
我的问题是如何在jquery中获取ID?还是有其他方法可以解决,但我希望使用jquery实现此特定目的.谢谢
您可以执行以下操作:
$("a[href='delete.php']").click(function(e){
var tr = $(this).closest('tr'),
id = tr[0].id;
// Put your AJAX call here
$.post('/delete/' + id, function(){
// Animate up, then remove
tr.slideUp(500, function(){
tr.remove();
});
});
});
closest()
在DOM树中向上移动,以查找与选择器匹配的祖先.在这种情况下,我们正在寻找第一个tr
.我还可以使用 parent()
并获得相同的结果.
i have a table with tr containing 10 td elements. The tr are generated dynamically. For eg
<tr id = "<?php echo $count; ?>" >
<td>name </td>
<td>info </td>
...
...
<td><a href="delete.php">delete</a></td>
</tr>
What i wish to do is when i click on delete link , using ajax go to delete page do the needful and then without page refresh delete the row.
My problem is how do i get the ID in jquery? or is there any other way i could work out but i wish to use jquery for this particular purpose. Thanks
You could do this:
$("a[href='delete.php']").click(function(e){
var tr = $(this).closest('tr'),
id = tr[0].id;
// Put your AJAX call here
$.post('/delete/' + id, function(){
// Animate up, then remove
tr.slideUp(500, function(){
tr.remove();
});
});
});
closest()
travels up the DOM tree looking for an ancestor that matches the selector. In this case, we were looking for the first tr
. I could have also used parent()
and gotten the same result.
这篇关于jQuery查找动态生成的tr标记的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!