0

想要复制特定命名表中的第一行或最后一行。我有大量的列,因此克隆是最好的方法。我发现了这个代码片段:

var i = 1;
$("button").click(function() ​​​{
  $("table tr:first").clone().find("input").each(function() {
    $(this).val('').attr('id', function(_, id) { return id + i });
  }).end().appendTo("table");
  i++;
})​;​

我的问题是,在哪里更改选择器以指定我要查找的表?如果你看一下这个小提琴,你会注意到添加一行确实只添加到一个表中,但它似乎从第一个可用表中克隆了该行。假设我只想复制行,table1因为我要附加到table1. 我该怎么做呢?

4

2 回答 2

1

Try this :

Append row for the "table" with id table "table1" :

$("button").click(function() ​​​{
  $("#table1 tr:first").clone().find("input").each(function() {
    $(this).val('').attr('id', function(_, id) { return id + i });
  }).end().appendTo("#table1");
  i++;
})​;​

Append row for the "table" with id table "table2" :

$("button").click(function() ​​​{
  $("#table2 tr:first").clone().find("input").each(function() {
    $(this).val('').attr('id', function(_, id) { return id + i });
  }).end().appendTo("#table2");
  i++;
})​;​
于 2013-06-09T04:48:06.710 回答
1

更新了定义了 from (#table1) 和 to (#table2) 表的代码示例。

var i = 1;
$("button").click(function() ​​​{
  $("table#table1 tr:first").clone().find("input").each(function() {
    $(this).val('').attr('id', function(_, id) { return id + i });
  }).end().appendTo("table#table2");
  i++;
})​;​
于 2013-06-09T04:16:57.383 回答