2012-03-05 129 views
0

我想克隆一个表格行,并生成一个ID为数组,因为用户可以插入n行。我面临的问题是,我在该表格行中有1个选择下拉选项。如何克隆一个选择以及其他单行的输入标记? (此代码生成2套排在一个时间)两个appendTo的堂妹(的感谢您的帮助克隆选择与其他输入标签一起?

$("#add").click(function() { 
    $("#comTable tr:eq(0)").clone().find("input").each(function() { 
     // creates array of ids if the user wants to add more than 1 row so it is N[1], etc 
     $(this).val('').attr('id', function(_, id) { 
      return id + 'N' + '[' + i + ']' 
     }); 
     $(this).val('').attr('name', function(_, name) { 
      return name + 'N' + '[' + i + ']' 
     }); 
    }).end().appendTo("#comTable"); 

    $("#comTable tr:eq(0)").clone().find("select").each(function() { 
     // creates array of ids if the user wants to add more than 1 row so it is N[1], etc 
     $(this).val('').attr('id', function(_, id) { 
      return id + 'N' + '[' + i + ']' 
     }); 
     $(this).val('').attr('name', function(_, name) { 
      return name + 'N' + '[' + i + ']' 
     }); 
    }).end().appendTo("#comTable"); 
    i++; 
});​ 

回答

1

你可以只选择inputselect元素,像这样:!

旧代码:

$("#comTable tr:eq(0)").clone().find("input").each(function() { 
    // creates array of ids if the user wants to add more than 1 row so it is N[1], etc 
    $(this).val('').attr('id', function(_, id) { 
     return id + 'N' + '[' + i + ']' 
    }); 
    $(this).val('').attr('name', function(_, name) { 
     return name + 'N' + '[' + i + ']' 
    }); 
}).end().appendTo("#comTable"); 

$("#comTable tr:eq(0)").clone().find("select").each(function() { 
    // creates array of ids if the user wants to add more than 1 row so it is N[1], etc 
    $(this).val('').attr('id', function(_, id) { 
     return id + 'N' + '[' + i + ']' 
    }); 
    $(this).val('').attr('name', function(_, name) { 
     return name + 'N' + '[' + i + ']' 
    }); 
}).end().appendTo("#comTable"); 

新代码:

$("#comTable tr:eq(0)").clone().find("input, select").each(function() { 
    // creates array of ids if the user wants to add more than 1 row so it is N[1], etc 
    $(this).val('').attr('id', function(_, id) { 
     return id + 'N' + '[' + i + ']' 
    }); 
    $(this).val('').attr('name', function(_, name) { 
     return name + 'N' + '[' + i + ']' 
    }); 
}).end().appendTo("#comTable"); 

"input, select"选择器。

编辑

你可以做到这一点,如果你想处理select S中的另一种方法不同是链不同的看法:

$("#comTable tr:eq(0)") 
    .clone() 
    .find("input") 
    .each(function() { 
      // creates array of ids if the user wants to add more than 1 row so it is N[1], etc 
      $(this).val('').attr('id', function(_, id) { 
       return id + 'N' + '[' + i + ']' 
      }); 
      $(this).val('').attr('name', function(_, name) { 
       return name + 'N' + '[' + i + ']' 
      }); 
     }) 
    .end() //End .find("input") 
    .find("select") 
    .each(function() { 
      // creates array of ids if the user wants to add more than 1 row so it is N[1], etc 
      $(this).val('').attr('id', function(_, id) { 
       return id + 'N' + '[' + i + ']' 
      }); 
      $(this).val('').attr('name', function(_, name) { 
       return name + 'N' + '[' + i + ']' 
      }); 
     }) 
    .end() //End .find("select") 
    .appendTo("#comTable"); 
i++; 

这种方式来消除多余的克隆和再次运行.find在新克隆的DOM元素上。

+0

谢谢!我正在尝试.find()方式,但最新的编辑似乎是最好的方式! – Nikhil 2012-03-05 17:59:43

+0

但是这在IE中不起作用!任何建议.. – Nikhil 2012-03-05 18:04:14

+0

@Nikhil - 它是否引发错误? – 2012-03-05 19:11:11