我在html中有一个表格行,其中包含3个输入字段,如下所示。

 <tr class="prototype">
<td>
      <input id="" type="text" value="" name="">
</td>
<td>
     <input id="" type="text" value="" name="">
</td>
<td>
     <input id="" type="text" value="" name="">
</td>

    </tr>


现在,我正在克隆此行,并尝试为新创建的行分配ID和名称。

如何为新创建的行分配ID和名称。

我正在克隆如下行。

        var master = $("table.myTable");
    var prot = master.find("tr.prototype").clone();
    prot.removeClass('prototype');
    prot.addClass("someClass");

         // for all inputs of prot row i need to assign unique ids and names.

    jQuery('table.myTable tr:last').before(prot);


我如何获取新创建的行的输入元素并在将行添加到表之前/之后分配ID和名称?

谢谢!

最佳答案

$(".someClass").children('input').each(function(index){
   $(this).attr('id',$(this).attr('id')+index);
   $(this).attr('name',$(this).attr('name')+index);
})


该代码仅将索引附加到输入的原始ID和名称上。

关于javascript - 为表格行的所有输入元素分配ID和名称?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18340586/

10-10 13:26