如何在不克隆表中输入元素值的情况下克隆表中的行? [英] How can I clone a row in a table without cloning the values of the input elements inside it?

查看:98
本文介绍了如何在不克隆表中输入元素值的情况下克隆表中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向表中添加一行.我发现我们可以使用 clone()方法来复制现有行.我的表在两个不同的<tr>元素中有两个文本输入.克隆最后一行也将复制文本输入中的值,这是我不想要的吗?如何在不复制值的情况下克隆行?

I am trying to add a row to a table. I found that we can use the clone() method to duplicate an existing row. My table has two text inputs in it in two different <tr> elements. Cloning the last row is also duplicating the values in my text inputs, which I don't want? How can I clone the row without duplicating the values?

这是我到目前为止所拥有的:

Here's what I have so far:

$("#table-1 tr:last").clone();

推荐答案

尝试一下:

var clone = $("#table-1 tr:last").clone().find('input').val('').end();

  • .clone() 最后一个<tr>
  • .find() 克隆中的<input>元素
  • <input>元素的 .val() 设置为''
  • 调用 .end() ,以便将克隆的<tr>存储在变量中,而不是<input>元素.
    • .clone() the last <tr>
    • .find() the <input> elements in the clone
    • set the .val() of the <input> elements to '',
    • call .end() so that the cloned <tr> is stored in the variable instead of the <input> elements.
    • 如果打算立即将其追加到表中,请在末尾添加 .insertAfter("#table-1 tr:last") .

      If you intend to append it to the table immediately, add .insertAfter("#table-1 tr:last") to the end.

      var clone = $("#table-1 tr:last").clone().find('input').val('').end().insertAfter("#table-1 tr:last");
      

      这篇关于如何在不克隆表中输入元素值的情况下克隆表中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆