jQuery 如何从jQuery中的表中删除当前行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2161991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:54:49  来源:igfitidea点击:

How to remove current row from table in jQuery?

jqueryhtml

提问by Amit

I have a table in html as follows

我在 html 中有一个表格如下

<table>
<tbody>
<tr>
<td>test content</td>
<td><input type="button" onClick="remove()"></td>
</tr>
....
...

</tbody>
</table>

now if the same pattern continues, i want to remove a row if a remove button is clicked on that row. how do i achieve the same with jQuery?

现在,如果相同的模式继续存在,如果在该行上单击删除按钮,我想删除该行。我如何使用 jQuery 实现相同的目标?

回答by cgp

Nicer:

更好:

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

More on closest()

更多关于最近()

<input type="button" onClick="$(this).closest('tr').remove();">

This has the benefit of working no matter what your HTML looks like in the cell.

无论您的 HTML 在单元格中是什么样子,这都有好处。

回答by Sarfraz

Try this:

尝试这个:

<input type="button" onClick="$(this).parent().parent().remove();">

Or you can make it more generic like this:

或者你可以像这样使它更通用:

<script>
  $(document).ready(function()
  {
    $(".btn").click(function(){
      $(this).parent().parent().remove();
    });
  });
</script>

<tr>
  <td><input type="button" class="btn"></td>
</tr>