简体   繁体   English

使用jQuery clone()的select2 v.4多选克隆'推荐'方式[销毁]删除行为

[英]select2 v.4 multiselect cloning with jQuery clone() 'recommended' way [destroy] removes behavior

Going about cloning a select2 multiselect 'by the book' ( as depicted here - $select.select2('destroy').clone().select2(); ) doesn't really have the desired behavior. 克隆一个select2多选择“按书”( 如此处所示 - $select.select2('destroy').clone().select2(); )确实没有所需的行为。

this fiddle shows that calling destroy() on the cloned element really destroys the source. 这个小提琴表明,在克隆的元素上调用destroy()确实会破坏源。 Only one clone is possible and the source becomes a simple 'list'. 只有一个克隆是可能的,并且源成为一个简单的“列表”。

$(".this-one").select2('destroy').clone().appendTo(".container").select2();

Is there a way to clone the multiselect while maintaining both the clone and the source in the same select2 format? 有没有办法在将克隆和源保持为相同的select2格式的情况下克隆多重选择?

Ok, i just saw that in this case you have to re-initialize the select2 element that you just destroyed: 好的,我只是看到在这种情况下,您必须重新初始化刚刚销毁的select2元素:

Fiddle 小提琴

$(".this-one").select2({
tags:true
})

I would love if anyone has a better solution to this approach, it doesn't feel too clean to be honest. 如果有人对这种方法有更好的解决方案,我会很乐意,说实话,它不会太干净。

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

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