简体   繁体   English

删除具有特定id的元素jquery

[英]Remove element jquery with a specific id

I want to remove a specific row with a unique id when the user click on a delete img. 我想在用户点击删除img时删除具有唯一ID的特定行。 It does not work when i select 'tr#'+id and then remove. 当我选择'tr#'+ id然后删除时,它不起作用。

Here is my code 这是我的代码

js JS

$("#page_body").on('click', 'a.delete_line', function(e) {
    var id=$(this).attr("id");
    $('tr#'+id).remove(); 
});

html HTML

<tr id="unitesbudg_roic_assetcenter.V01.aaf.filename">
<td class="value">ANY</td><td class="value">MAIN </td>
<td class="value">false</td>
<td class="value">unitesbudg_roic_assetcenter.V01.aaf.filename</td>
<td class="value">unitesbudg_roic_assetcenter.txt</td>
<td class="value">undefined</td>
<td><a href="#" class="delete_line" id="unitesbudg_roic_assetcenter.V01.aaf.filename"><img src="img/delete.png"></a></td>
</tr>

Also if you can advice me on best practices for such a feature. 此外,如果您可以就此功能的最佳实践向我提出建议。

Thx a lot in advance 提前做了很多

In HTML IDs must be unique. 在HTML中, IDs必须是唯一的。 You can use .closest() to find the parent tr 您可以使用.closest()来查找父tr

Just use 只是用

 $(this).closest("tr").remove();

DEMO DEMO

Your JQuery should look something more like this: 你的JQuery应该看起来像这样:

$("#page_body").on('click', 'a.delete_line', function(e) {
  $(this).closest('tr').remove();
});

尝试这个:

   $('#page_body tr:last').remove();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM