代码之家  ›  专栏  ›  技术社区  ›  Pawan

从表中删除行

  •  0
  • Pawan  · 技术社区  · 6 年前

    删除时,如果只有一行,

    我试过以下方法

    $(".butnBorrar").click(function(event){
    
       if($("#MyTable tr").length==2)
       {
        $(this).find('td:last-child').remove();
       }
       else
       {
      $(this).closest('tr').remove();
      }
    });
    

    https://jsfiddle.net/44avhkas/33/

    2 回复  |  直到 6 年前
        1
  •  1
  •   Ctznkane525    6 年前

    请试试这个。

    $(".butnBorrar").click(function(event){
    
    
      $(this).closest('tr').remove();
      if($("#MyTable tr").length==2) // accounts for table header
      {
        //window.alert("In Here");
            $("#MyTable tr").find('td:last-child').remove();
      }
    
    });
    
        2
  •  0
  •   Lord Grosse Jeanine    6 年前
    $(".butnBorrar").click(function(event) {
        //test the number or rows on tbody only
        if($("#MyTable tbody tr").length <= 1) {
            //use closest to get the parent tr
            //then hide the button
            $(this).closest('tr').find('td.total button').hide();
        }
        else {
            //else remove row
            $(this).closest('tr').remove();
        }
    });