简体   繁体   English

有什么办法可以在jQuery中克隆sumoselect下拉列表

[英]Is there any way to clone sumoselect dropdown in jquery

I am using jquery sumoselect library for showing multiselect dropdown. 我正在使用jQuery sumoselect库显示多选下拉列表。 Now i want to clone this drop down on click of button. 现在,我想在单击按钮时克隆此下拉列表。 Is there any way to achieve this? 有什么办法可以做到这一点?

$('#chtml').clone().attr('id', 'newid').appendTo('p'); 

I have tried above code where #chtml is the id of td in which my multiselect drop down present and i want to copy this into <p></p> tag. 我已经尝试了上面的代码,其中#chtml是td的ID,其中我的多选下拉列表存在,我想将此复制到<p></p>标记中。

There seems to be 2 problems with your attempt.. 您的尝试似乎有2个问题。

  1. The library creates <p> tags, so simply appending to $("p") is literally going to insert the select element into the existing SumoSelect instance. 该库创建<p>标记,因此只需将$("p")附加到字面上,就可以将select元素插入现有的SumoSelect实例中。
  2. The library hides the original <select> so you're appending an invisible select element anyway. 该库将隐藏原始的<select>因此无论如何您都将添加不可见的select元素。 Solve this by calling SumoSelect() on the clone. 通过在克隆上调用SumoSelect()解决此问题。

PS. PS。 There's really no need to give it a new id, just remove the existing one. 确实不需要给它一个新的ID,只需删除现有的ID。

 $("#chtml").SumoSelect(); $("#chtml").clone().removeAttr("id").appendTo("#cloneGoesHere").SumoSelect(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.sumoselect/3.0.2/jquery.sumoselect.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/jquery.sumoselect/3.0.2/sumoselect.css" rel="stylesheet" /> <select name="somename" id="chtml" tabindex="-1"> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="mercedes">Mercedes</option> <option value="audi">Audi</option> </select> <p id=cloneGoesHere></p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM