jquery Find ID of dynamically generated tr tag

老子叫甜甜 提交于 2019-12-11 06:32:06

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/2046895/jquery-find-id-of-dynamically-generated-tr-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!