【问题标题】:dynamically add remove rows in table using jquery使用jquery在表中动态添加删除行
【发布时间】:2010-02-09 11:51:21
【问题描述】:

我的表格如下:

    <table id="dataTable">
            <thead>
                <tr><th>Name</th>
               <th>Value</th></tr>
            </thead>
<TR><TD>Scooby Doo</TD><TD>6</TD><TD><INPUT TYPE="Button"  onClick="AddRow()" VALUE="Add Row"></TD></TR>
</table>

单击“添加行”按钮时,我需要将按钮更改为删除按钮并在第一行插入新行。第一行必须包含与代码中相同的内容。我该怎么做?

点击删除按钮时,我必须能够删除删除按钮所属的行吗?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    希望对你有帮助

    $(function(){
      $("input[type='button'].AddRow").toggle(
         function(){
           var el = $(this);
           el.closest('tr').clone(true).prependTo(el.closest('table'));
           el.attr("value", "Delete row");
         },
         function(){ 
           $(this).closest('tr').remove();          
      });
    });
    
    <table id="dataTable" border="1">
        <thead>
            <tr>
                <th>
                    Name
                </th>
                <th>
                    Value
                </th>
            </tr>
        </thead>
        <tr>
            <td>
                Scooby Doo
            </td>
            <td>
                6
            </td>
            <td>
                <input type="Button" value="Add Row" class="AddRow">
            </td>
        </tr>
     </table>
    

    Working Demo

    【讨论】:

      【解决方案2】:

      类似的东西?

      $('#dataTable thead').prepend('<tr><th>Name</th><th>Value</th></tr>');
      

      对于删除:

      $('#dataTable thead tr').click(function() {
      $(this).hide(); //hide the row, this doesn't delete it.
      });
      

      .

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-02
        • 2016-02-07
        • 1970-01-01
        • 2016-10-29
        • 2013-04-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多