【发布时间】:2016-02-10 12:58:36
【问题描述】:
我有这样的表格
<tr id='actionRow'>
<td>
<input type="text" name='actionInput[0][action]' id="actionInput" placeholder='Action' class="form-control"/>
</td>
<td>
<input type="text" name='actionInput[0][deliveryDate]' id="dateInput" placeholder='Completion Date' class="form-control dateControl"/>
</td>
</tr>
您可以看到我的输入名称是一个二维数组。我让用户可以选择向表中添加另一行,这将克隆上面的内容。目前,我的代码是这样的
$("table tr").eq(1).clone().find("input").each(function() {
$(this).closest('tr').attr('id', 'actionRow'+i);
$(this).attr({
'id': function(_, id) {
return id + i
},
'name': function(_, id) {
return $(this).attr('name').replace('['+(id-1)+']', '['+id+']')
},
'value': ''
});
if ($(this).hasClass('dateControl')) {
$(this).val("");
}
}).end().appendTo("table");
我在更新克隆项目的名称属性时遇到问题。对于第一个克隆的行,html 基本上应该如下所示
<tr id='actionRow'>
<td>
<input type="text" name='actionInput[1][action]' id="actionInput1" placeholder='Action' class="form-control"/>
</td>
<td>
<input type="text" name='actionInput[1][deliveryDate]' id="dateInput1" placeholder='Completion Date' class="form-control dateControl"/>
</td>
</tr>
所以数组元素变为1。下一个克隆的行将有2等等。
使用我提供的 JQuery 代码,为什么替换不适合我?
谢谢
【问题讨论】:
标签: javascript jquery html