0

我正在克隆表中的最后一行,但我需要为每个输入提供唯一的多维名称......

    <tr>
<td><input type="text" name='input[1][Name]'></input></td>
<td><input type="text" name='input[1][address]'></input></td>
<td><input type="text" name='input[1][contactInfo]'></input></td>
</tr>

下一行应该是

<tr>
<td><input type="text" name='input[2][Name]'></input></td>
<td><input type="text" name='input[2][address]'></input></td>
<td><input type="text" name='input[2][contactInfo]'></input></td>
</tr>

...... jQuery

  $(".alternativeRow").click(function(){
i=2;
                $("table tr:last").clone().find("input").each(function() {
                    $(this).attr({
                        'id': function(_, id) { return id + i },
                        'name': function(_, name) { return name + i },
                        'value': ''               
                    });
                }).end().appendTo("table");
                i++;
      });
4

2 回答 2

1

对于多维名称,无需为名称提供显式索引。直接可以这样写。

<input type="text" name='input[][Name]'></input>

当您在服务器端收到表单数据时,这将自动获取索引号。

于 2012-05-28T10:18:30.373 回答
0
 $(".alternativeRow").click(function(){
i=2;
                $("table tr:last").clone().find("input").each(function() {
                   this.name = this.name.replace(/\[(\d+)\]/, function(){
                        return '[' + i +']';
                    });
                })
                .end().appendTo("table");
                i++;
      });
于 2012-05-29T08:13:01.167 回答