【问题标题】:copy select element in a table复制表格中的选择元素
【发布时间】:2014-12-18 21:40:48
【问题描述】:

我有一个动态表,当用户单击按钮时,我会在其中动态添加行。

我的按钮调用以下函数:

<input type="button" value="Add" onclick="addRowTuy($('#AmonttableTuy tbody'), 'AmontTuy');" />

我的函数向我的表中添加了一个新行。它包括文本字段、输入和选择。

我对创建新输入和文本没有问题,但对于选择,我想复制现有选择并更改其 ID。那是我的问题。

这是我在表中创建新行的函数:

function addRowTuy(table, value)
{
var nbrow = table.children().size();
    table.append( "<tr>"+
    "<td>"+(nbrow+1)+"</td>"+
    "<td><input id='"+value+(nbrow+1)+"Diam"+"'/></td>"+
    "<td><input id='"+value+(nbrow+1)+"Long"+"'/></td>"+
    "<td>"+$("#AmontTuy1Type")+"</td>"+ //This is where I want copy the select element
    "<td><input id='"+value+(nbrow+1)+"Rugo"+"'/></td>"+
"</tr>");
}

你有什么想法吗?

【问题讨论】:

  • 看看.clone
  • 我看过.clone并尝试过,但是我使用它时没有成功

标签: javascript jquery html


【解决方案1】:

这样的东西应该可以工作

"<td>"+$("#AmontTuy1Type").clone().attr('id', 'newID').html() + 

您需要克隆并将其 html 添加到您的字符串中,而不是元素本身。或者创建一个 td 元素并将克隆附加到它。

更新:

是的,你是对的,html() 只接受选项,而不是选择本身。试试这个:

"<td>"+"<select id='newID'>" + $("#AmontTuy1Type").html() + "</select>" +

【讨论】:

  • 这里只复制select的选项而不是所有的select。很奇怪,因为“AmontTuy1Type”是select的id
  • 完美就是我所需要的。谢谢
【解决方案2】:

有不同的方法可以做到这一点。以下链接将对您有所帮助。

How to add (clone) form fields using jQuery and increment ids and names

jquery clone form fields and increment id

【讨论】:

    【解决方案3】:

    我是这样做的

    $("#AmontTuy1Type").parent().clone().attr('id', 'newID').html()
    

    把select复制好了,但是这个的id没变?

    【讨论】:

      猜你喜欢
      • 2018-06-28
      • 1970-01-01
      • 1970-01-01
      • 2017-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多