首页 文章

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

提问于
浏览
273

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

16 回答

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

    你是对的:

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

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

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

    如果你没有 id ,你可以使用任何jQuery的plethora of selectors .

  • 7

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

    $(".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();
    });
    

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

  • 2

    您可以使用:

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

    用于查找元素的父表行 .

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

    • 编辑 - 有人指出问题是关于删除行......
    $($(this).closest("tr")).remove()
    

    如下所述,您可以简单地做到:

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

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

  • 1

    容易..试试这个

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

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

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

    *上述代码取自this jQuery Howto post .

  • 1

    以下是可接受的:

    $('#myTableRow').remove();
    
  • 94
    function removeRow(row) {
        $(row).remove();
    }
    
    <tr onmousedown="removeRow(this)"><td>Foo</td></tr>
    

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

  • -1

    试试这个尺码

    $(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>
    

    see it in action

  • 54

    另一个 empty()

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

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

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

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

  • 15

    如果您有这样的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,但无论如何您都可以删除它 .

  • 7

    我很欣赏这是一个老帖子,但我也希望这样做,并且发现接受的答案对我不起作用 . 假设JQuery已经移动了,因为这是写的 .

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

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

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

  • 3

    如果您使用的是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});

    Credits to this post

  • 0

    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');
    

相关问题