多维数组中的增量键,格式为';使用jqueryclone创建输入字段

多维数组中的增量键,格式为';使用jqueryclone创建输入字段,jquery,Jquery,我正试图从这两个答案中拼凑出信息,但这两个答案似乎都没有涵盖这个问题,我可以根据自己的情况推断并着手解决。所以,如果这看起来像是重复的,请不要惩罚我;我想不是 任何帮助都将不胜感激。理想情况下,我还想添加一个“删除行”按钮。从正则表达式中取出$符号,如下所示: $("#newRowButton").live('click',function(e) { e.preventDefault(); $("table.tablearray tr:last") .clone()

我正试图从这两个答案中拼凑出信息,但这两个答案似乎都没有涵盖这个问题,我可以根据自己的情况推断并着手解决。所以,如果这看起来像是重复的,请不要惩罚我;我想不是


任何帮助都将不胜感激。理想情况下,我还想添加一个“删除行”按钮。

从正则表达式中取出
$
符号,如下所示:

$("#newRowButton").live('click',function(e) {
    e.preventDefault();
  $("table.tablearray tr:last")
      .clone()
      .find(':input')
      .each(function(){
            this.name = this.name.replace(/\[(\d+)\]/, function(str,p1){
                return '[' + (parseInt(p1,10)+1) + ']';
            });
        })
    .end()
    .appendTo("table")
});
$
强制它在名称的末尾查找
[0]
,而该名称不是您拥有的名称


此处的工作演示:

您的正则表达式中有一个小错误:

this.name = this.name.replace(/\[(\d+)\]$/,
这仅在元素名称的结尾处匹配
[0]
,因为
$
表示“字符串的结尾”。如果删除该选项,转换将正常工作:

this.name = this.name.replace(/\[(\d+)\]/,
                                      function(str,p1){
                                          return '[' + (parseInt(p1,10)+1) + ']';
                                      });

你能修改html(添加和属性)吗?多么漂亮的答案。非常适合我的需要,谢谢:)
$("#newRowButton").live('click',function(e) {
    e.preventDefault();
  $("table.tablearray tr:last")
      .clone()
      .find(':input')
      .each(function(){
            this.name = this.name.replace(/\[(\d+)\]/, function(str,p1){
                return '[' + (parseInt(p1,10)+1) + ']';
            });
        })
    .end()
    .appendTo("table")
});
this.name = this.name.replace(/\[(\d+)\]$/,
this.name = this.name.replace(/\[(\d+)\]/,
                                      function(str,p1){
                                          return '[' + (parseInt(p1,10)+1) + ']';
                                      });