繁体   English   中英

jQuery使用另一种方法克隆表行

[英]Clone table row using another approach by jquery

我有一张桌子,我在图标上克隆了一张桌子。

现在,我所做的如下:

<h3 class="table-add glyphicon glyphicon-plus"></h3>
<table class="table">
   <tr id= "1">
        <td> <input name = "textfield" type="text" class="form-control" value = "1" > </td>
        <td> <input name= "numberfield" type="number" class="form-control" value = "2"> </td>
    </tr>


   <tr id= "3">
        <td> <input name = "textfield" type="text" class="form-control" value = "1" > </td>
        <td> <input name= "numberfield" type="number" class="form-control" value = "2"> </td>
    </tr>


   <tr id= "4">
        <td> <input name = "textfield" type="text" class="form-control" value = "1" > </td>
        <td> <input name= "numberfield" type="number" class="form-control" value = "2"> </td>
    </tr>

   <tr id = "5" class="hide">
        <td> <input name = "textfield" type="text" class="form-control" value = "1" > </td>
        <td> <input name= "numberfield" type="number" class="form-control" value = "2"> </td>           
   </tr>
</table>

现在,我通过获取隐藏行来克隆行:

      var $TABLE = $('#table');

      $('.table-add').click(function () {

        var $clone = $TABLE.find('tr.hide').clone(true).removeClass('hide table-line');            
        // //Assigning every table row a unique ID
        var max=0;
        $('table').find('tr').each(function(){
            var id=parseInt($(this).attr('id'));
            if (id>=max){
               max = id;
             }
         });
         //cloning row with new ID  
         $clone.attr('id', parseInt(max)+1);
         $clone.find('input.myinput').tagsinput('removeAll');
         $TABLE.find('table').append($clone);
      });

这就是我正在做的-克隆隐藏的行,并通过从$ clone克隆的行中删除隐藏的类将其追加到表中,这也是一种不错的方法。 但是我不想要这样的东西,我想克隆一个隐藏的行,该行现在将变得可见,并且克隆的隐藏行的行现在必须被隐藏。 如何去做。

谢谢

我不知道您要怎么做...但是,我建议您使用jQuery插件来更轻松地实现。 我在下面放置了我之前使用过的一个。

jQuery插件复制元素:

https://github.com/fire1/DuplicateElement

现场演示:

http://fire1.eu/DuplicateElement/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM