【问题标题】:Select first row in a named HTML table using JQuery for cloning使用 JQuery 选择命名 HTML 表中的第一行进行克隆
【发布时间】:2013-06-09 04:06:22
【问题描述】:

想要复制特定命名表中的第一行或最后一行。我有大量的列,因此克隆是最好的方法。 I found this code snippet:

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++;
})​;​

我的问题是,在哪里更改选择器以指定我要查找的表?如果您take a look at this fiddle 您会注意到添加一行确实 add 到一个表中,但它似乎是从第一个可用表中克隆该行。假设我只想从table1 复制行,因为我要附加到table1。我该怎么做?

【问题讨论】:

    标签: jquery html-table row cloning


    【解决方案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++;
    })​;​
    

    【讨论】:

    • 感谢一百万!您在特定表中查找特定行的语法太棒了$("table#table1 tr:first").clone()。正是我需要的!
    【解决方案2】:

    试试这个:

    为 id 为“table1”的“table”添加行:

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

    为 id 为 "table2" 的 "table" 添加行:

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

    【讨论】:

    • 感谢您的帮助!从特定表中唯一标识特定行的语法如下:"table#table1 tr:first"。很接近!你确实让我走上了正轨。
    猜你喜欢
    • 2013-10-24
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    相关资源
    最近更新 更多