使用jQuery删除表行的最佳方法是什么?

有帮助吗?

解决方案

你是对的:

$('#myTableRow').remove();

如果您的行有id,则可以正常工作,例如:

<tr id="myTableRow"><td>blah</td></tr>

如果您没有<=>,则可以使用jQuery的众多选择器中的任何一个

其他提示

$('#myTable tr').click(function(){
    $(this).remove();
    return false;
});

假设你的表格中有一个数据单元格内有一个按钮/链接,那么这样就可以了......

$(".delete").live('click', function(event) {
    $(this).parent().parent().remove();
});

这将删除单击的按钮/链接的父级的父级。你需要使用parent(),因为它是一个jQuery对象,而不是一个普通的DOM对象,你需要使用parent()两次,因为按钮位于一个数据单元内,它位于一行....这是你想要删除什么。 $(this)是单击的按钮,所以只需删除按钮:

$(this).remove();

虽然这会删除数据单元格:

    $(this).parent().remove();

如果您只想点击该行上的任意位置以删除它,则可以使用此类功能。您可以轻松修改此选项以提示用户或仅在双击时工作:

$(".delete").live('click', function(event) {
    $(this).parent().remove();
});

希望有所帮助......我在这方面有点挣扎。

您可以使用:

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

用于查找元素的父表行。

它比parent()。parent()更优雅,这是我开始做的事情,很快就学会了我的错误。

- 编辑 - 有人指出,问题是关于删除行...

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

如下所述,您可以这样做:

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

类似的代码片段可用于许多操作,例如在多个元素上触发事件。

轻松..试试这个

$("table td img.delete").click(function () {
    $(this).parent().parent().parent().fadeTo(400, 0, function () { 
        $(this).remove();
    });
    return false;
});

以下是否可以接受:

$('#myTableRow').remove();
function removeRow(row) {
    $(row).remove();
}

<tr onmousedown="removeRow(this)"><td>Foo</td></tr>

也许这样的事情也可以起作用?我没有尝试用<!>“这个<!>”做某事,所以我不知道它是否有效。

您所要做的就是从表中删除表格行(<tr>)标记。例如,以下是从表中删除最后一行的代码:

  

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

*以上代码取自 此jQuery Howto帖子

尝试这个尺寸

$(this).parents('tr').first().remove();

完整列表:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
  <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function() {
        $('.deleteRowButton').click(DeleteRow);
      });

    function DeleteRow()
    {
      $(this).parents('tr').first().remove();
    }
  </script>
</head>
<body>
  <table>
    <tr><td>foo</td>
     <td><a class="deleteRowButton">delete row</a></td></tr>
    <tr><td>bar bar</td>
     <td><a class="deleteRowButton">delete row</a></td></tr>
    <tr><td>bazmati</td>
     <td><a class="deleteRowButton">delete row</a></td></tr>
  </table>
</body>
</html>

看到它的实际效果

另一个empty()

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

如果要删除的行可能会更改,则可以使用此行。只需将此功能传递给您要删除的行#。

function removeMyRow(docRowCount){
   $('table tr').eq(docRowCount).remove();
}
$('tr').click(function()
 {
  $(this).remove();
 });

我认为你会尝试上面的代码,因为它工作,但我不知道它为什么有效,然后整个表被删除。我也试图通过单击该行删除该行。但找不到确切的答案。

如果您有像这样的HTML

<tr>
 <td><span class="spanUser" userid="123"></span></td>
 <td><span class="spanUser" userid="123"></span></td>
</tr>

其中userid="123"是一个自定义属性,您可以在构建表时动态填充该属性,

你可以使用像

这样的东西
  $(".spanUser").live("click", function () {

        var span = $(this);   
        var userid = $(this).attr('userid');

        var currentURL = window.location.protocol + '//' + window.location.host;
        var url = currentURL + "/Account/DeleteUser/" + userid;

        $.post(url, function (data) {
          if (data) {
                   var tdTAG = span.parent(); // GET PARENT OF SPAN TAG
                   var trTAG = tdTAG.parent(); // GET PARENT OF TD TAG
                   trTAG.remove(); // DELETE TR TAG == DELETE AN ENTIRE TABLE ROW 
             } else {
                alert('Sorry, there is some error.');
            }
        }); 

     });

因此,在这种情况下,您不知道TR标签的类或ID,但无论如何您都可以删除它。

我很欣赏这是一篇旧帖子,但我也希望这样做,并且发现接受的答案对我不起作用。假设JQuery已经开始了,因为这是写的。

无论如何,我发现以下内容对我有用:

$('#resultstbl tr[id=nameoftr]').remove();

不确定这是否有助于任何人。我上面的例子是一个更大的函数的一部分,因此不会将它包装在事件监听器中。

如果您使用的是Bootstrap Tables

将此代码段添加到bootstrap_table.js

BootstrapTable.prototype.removeRow = function (params) {
    if (!params.hasOwnProperty('index')) {
        return;
    }

    var len = this.options.data.length;

    if ((params.index > len) || (params.index < 0)){
        return;
    }

    this.options.data.splice(params.index, 1);

    if (len === this.options.data.length) {
        return;
    }

    this.initSearch();
    this.initPagination();
    this.initBody(true);
};

然后在var allowedMethods = [

添加'removeRow'

最后你可以使用$("#your-table").bootstrapTable('removeRow',{index:1});

此帖子的致谢

id现在不是一个好的选择器。您可以在行上定义一些属性。你可以使用它们作为选择器。

<tr category="petshop" type="fish"><td>little fish</td></tr>
<tr category="petshop" type="dog"><td>little dog</td></tr>
<tr category="toys" type="lego"><td>lego starwars</td></tr>

你可以使用func来选择这样的行(ES6):

const rowRemover = (category,type)=>{
   $(`tr[category=${category}][type=${type}]`).remove();
}

rowRemover('petshop','fish');
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top