【问题标题】:What is the best way to remove a table row with jQuery?用 jQuery 删除表格行的最佳方法是什么?
【发布时间】:2010-09-15 07:41:33
【问题描述】:

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

【问题讨论】:

    标签: javascript jquery html-table


    【解决方案1】:

    以下是否可以接受:

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

    【讨论】:

    • 这会删除整个表格,而不仅仅是表格的行。如果您只想清除行,它不会留下任何可附加的内容。
    【解决方案2】:

    你是对的:

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

    如果您的行有id,这很好用,例如:

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

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

    【讨论】:

    • 这样做确实意味着每行需要一个 id,这可能会增加很多开销。 jQuery 允许其他更惯用的方法(对于 jQuery 的方法),继续阅读有更多建议。
    • 对于动态内容,使行 id unique 你可以通过为 php &lt;tr id="myTableRow&lt;?php echo $id; ?&gt;"&gt; 附加一些独特的东西来做到这一点,并且在删除它时你必须生成这个 id :)
    【解决方案3】:
    function removeRow(row) {
        $(row).remove();
    }
    
    <tr onmousedown="removeRow(this)"><td>Foo</td></tr>
    

    也许这样的事情也能奏效?我没有尝试用“this”做点什么,所以我不知道它是否有效。

    【讨论】:

    • 好吧,我会说当你点击它时如果行消失会有点奇怪。目前我在该行中有一个链接可以删除该行。
    【解决方案4】:
    $('#myTable tr').click(function(){
        $(this).remove();
        return false;
    });
    

    一个更好的

    $("#MyTable").on("click", "#DeleteButton", function() {
       $(this).closest("tr").remove();
    });
    

    【讨论】:

      【解决方案5】:
      $('tr').click(function()
       {
        $(this).remove();
       });
      

      我想你会尝试上面的代码,因为它可以工作,但我不知道为什么它会工作一段时间,然后整个表都被删除了。我也试图通过单击该行来删除该行。但找不到确切的答案。

      【讨论】:

      • 我不确定你是否已经尝试过 $('tr').live("click",function(){ $(this).remove();});
      【解决方案6】:

      假设您在表格的数据单元格中有一个按钮/链接,这样的事情就可以解决问题...

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

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

      【讨论】:

      • 如果链接不是直接在 td 内,而是在它周围有一个跨度怎么办?我认为这样做会更好 $(this).parents('tr').remove();让它自己爬上 DOM 树,找到 tr,然后删除它。
      • 那也行。这完全取决于您的按钮/链接在 DOM 中的哪个位置,这就是为什么我给出了这么多示例和这么长的解释。
      • 你也可以使用$(this).parents('tr')
      • .live 在 jQuery 1.7 中被弃用,并在 1.9 中被移除。见jQuery.live
      【解决方案7】:

      您所要做的就是从表格中删除表格行 (&lt;tr&gt;) 标记。例如这里是从表中删除最后一行的代码:

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

      *以上代码取自this jQuery Howto post

      【讨论】:

        【解决方案8】:

        你可以使用:

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

        用于查找元素的父表行。

        它比 parent().parent() 更优雅,这是我开始做的,很快就发现了我的方法的错误。

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

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

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

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

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

        【讨论】:

        • 再简洁一点:$(this).closest("tr").remove()
        【解决方案9】:

        简单..试试这个

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

        【讨论】:

        • 我会将$(this).parent().parent().parent() 更改为$(this).closest('tr')。它更强大,更清楚地显示了您正在选择的内容。
        【解决方案10】:

        试试这个尺寸

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

        【讨论】:

          【解决方案11】:

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

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

          【讨论】:

            【解决方案12】:

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

            【讨论】:

            • 认为 live 现在已弃用,支持 on, $(".spanUser").on('click', function (){
            【解决方案13】:

            empty() 的另一个:

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

            【讨论】:

            • 这不是删除所有 而不是 吗?我猜浏览器可能会自动删除 ,但我怀疑这不能保证。
            【解决方案14】:

            我很欣赏这是一篇旧帖子,但我也想做同样的事情,但发现接受的答案对我不起作用。假设 JQuery 自从写完之后就继续前进了。

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

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

            不确定这是否对任何人有帮助。我上面的示例是一个更大函数的一部分,因此没有将其包装在事件侦听器中。

            【讨论】:

              【解决方案15】:

              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>
              

              你可以使用一个函数来选择这样的行(ES6):

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

              【讨论】:

                【解决方案16】:

                如果您使用的是引导表

                将此代码 sn-p 添加到您的 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

                【讨论】:

                  【解决方案17】:

                  从表中删除行的最简单方法:

                  1. 使用唯一 ID 删除表格行。
                  2. 根据该行的顺序/索引删除。例如:删除第三或第五行。

                  例如:

                   <table id='myTable' border='1'>
                      <tr id='tr1'><td>Row1</td></tr>
                      <tr id='tr2'><td>Row2</td></tr>
                      <tr id='tr3'><td>Row3</td></tr>
                      <tr id='tr4'><td>Row4</td></tr>
                      <tr id='tr5'><td>Row5</td></tr>
                    </table>
                  
                  //======REMOVE TABLE ROW=========
                  //1. remove spesific row using its ID
                  $('#tr1').remove();
                  
                  //2. remove spesific row using its order or index.
                  //row index started from 0-n. Row1 index is 0, Row2 index is 1 and so on.
                  $('#myTable').find('tr:eq(2)').remove();//removing Row3
                  

                  【讨论】:

                    猜你喜欢
                    • 2015-06-05
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2020-12-10
                    • 2010-09-09
                    • 2011-10-03
                    • 2012-04-17
                    相关资源
                    最近更新 更多