使用jquery克隆元素

使用jquery克隆元素,jquery,replace,Jquery,Replace,我正在开发一个表单,用户可以根据需要添加行。有一个名为“AddRow”的链接,当它单击时,我想克隆一个隐藏表并更改它的名称值,一些类名。谁能帮我一下吗 <table class="montaj montaj-satir"> <tr> <td width="62px"><input type="text" name="oper[--number--][kodu]" size="4" maxlength="4" /></t

我正在开发一个表单,用户可以根据需要添加行。有一个名为“AddRow”的链接,当它单击时,我想克隆一个隐藏表并更改它的名称值,一些类名。谁能帮我一下吗

<table class="montaj montaj-satir">
    <tr>
        <td width="62px"><input type="text" name="oper[--number--][kodu]" size="4" maxlength="4" /></td>
        <td width="38px">
            <input type="checkbox" name="oper[--number--][onay]" value="1" />
            <input type="hidden" name="oper[--number--][terminal_hatasi]" value="0" />
            <input type="hidden" name="oper[--number--][soket_hatasi]" value="0" />
            <input type="hidden" name="oper[--number--][montaj_hatasi]" value="0" />
            <input type="hidden" name="oper[--number--][yon_hatasi]" value="0" />
            <input type="hidden" name="oper[--number--][push_hatasi]" value="0" />
            <input type="hidden" name="oper[--number--][olcu_hatasi]" value="0" />
            <input type="hidden" name="oper[--number--][bant_hatasi]" value="0" />
            <input type="hidden" name="oper[--number--][vida_hatasi]" value="0" />
            <input type="hidden" name="oper[--number--][komponent_hatasi]" value="0" />
        </td>
        <td width="62px"><span class="terminal_hatasi--number--">0</span> <a href="#" class="add hata-ekle" id="terminal_hatasi--number--">&nbsp; &nbsp; &nbsp;</a></td>
        <td width="62px"><span class="soket_hatasi--number--">0</span> <a href="#" class="add hata-ekle" id="soket_hatasi--number--">&nbsp; &nbsp; &nbsp;</a></td>
        <td width="62px"><span class="montaj_hatasi--number--">0</span> <a href="#" class="add hata-ekle" id="montaj_hatasi--number--">&nbsp; &nbsp; &nbsp;</a></td>
        <td width="62px"><span class="yon_hatasi--number--">0</span> <a href="#" class="add hata-ekle" id="yon_hatasi--number--">&nbsp; &nbsp; &nbsp;</a></td>
        <td width="62px"><span class="push_hatasi--number--">0</span> <a href="#" class="add hata-ekle" id="push_hatasi--number--">&nbsp; &nbsp; &nbsp;</a></td>
        <td width="62px"><span class="olcu_hatasi--number--">0</span> <a href="#" class="add hata-ekle" id="olcu_hatasi--number--">&nbsp; &nbsp; &nbsp;</a></td>
        <td width="62px"><span class="bant_hatasi--number--">0</span> <a href="#" class="add hata-ekle" id="bant_hatasi--number--">&nbsp; &nbsp; &nbsp;</a></td>
        <td width="62px"><span class="vida_hatasi--number--">0</span> <a href="#" class="add hata-ekle" id="vida_hatasi--number--">&nbsp; &nbsp; &nbsp;</a></td>
        <td width="62px"><span class="komponent_hatasi--number--">0</span> <a href="#" class="add hata-ekle" id="komponent_hatasi--number--">&nbsp; &nbsp; &nbsp;</a></td>
    </tr>
    <tr>
        <td colspan="2">Kablo Sıra No</td>
        <td colspan="4"><input type="text" name="oper[--number--][kablo_sira_no]" value="" /></td>
        <td colspan="2">Açıklama</td>
        <td colspan="3"><input type="text" name="oper[--number--][aciklama]" value="" /></td>
    </tr>
</table>

0
0
0
0
0
0
0
0
0
卡布罗·斯奥拉号
Açıklama
包含
.hata ekle
类的链接,增加隐藏输入的值及其旁边的范围

如果需要新行,用户应单击“添加行”链接,并显示新克隆的
.montaj
表,但
--编号--
应替换为唯一的编号

我该怎么做

谢谢…

好的

我不了解整个过程,但我可以给你一些建议:

  • 第一种方法:编写一个函数,在参数中输入所需的数字,并使用jQuery实用程序创建表:

    function addTable(number) {
      var table = $('<table>');
      var tr = $('<tr>').appendTo(table);
      var td = $('<td>').appendTo(tr);
      var span = $('<span>').appendTo(td).addClass('terminal_hatasi'+number)
      ...
    }
    
  • 我觉得还有更多的办法,这两个都是我首先想到的。这些不一定是更好的

    function addTable(number) {
      var clone = $('table.montaj').first().clone();
    
      clone.find('input').each(function(item){
        var name = $(this).attr('name');
        $(this).attr('name',name.replace(/\d/,number));
      });
    
      ...
    
      $(body).append(clone);
    }