[英]Cloning Select2 with Jquery
I am having an issue with cloning a div with a select2 drop down. 我在使用select2下拉列表克隆div时遇到问题。
When the first one is shown on the initial page load, the select2 works fine. 当第一个显示在初始页面加载时,select2工作正常。 When I click the button to clone it, the next select2 doesn't.
当我单击按钮来克隆它时,下一个select2不会。 It's like it's disabled and doesn't work.
这就像它被禁用而且不起作用。
My code is as follows; 我的代码如下;
<div class="input-main white_bg cloneneedsanalysis">
<div class="action-input clone-needs-analysis">
<div class="action-input-white-shorter">
<div class="inner-sub-title-less-padding">
<p>Audience Analysis</p>
</div>
<div class="col-md-12">
<div class="form-group">
<select class="select2 select2-multiple need_analysis_selector" multiple="multiple" data-placeholder="Choose ..." name="needs_analysis[]">
<option value="">-- Choose Audience Categories --</option>
<option value="536"> Finance - - Cash collection</option>
<option value="537">IT - - Comms IT</option>
<option value="538">Strategy - - Strategy team 1</option>
</select>
</div>
</div>
</div>
<div class="action-input-plus-needs-analysis">
<a href="#" class="clone_needs_analysis_button">
<img src="http://www.test.com/assets/images/plus.jpg"></a>
</div>
<div class="action-input-remove-needs-analysis">
<a href="#" class="remove_needs_anaylsis_button">
<img src="http://www.test.com/assets/images/minus.jpg"></a>
</div>
</div>
</div>
$(document).on('click', '.clone_needs_analysis_button', function(e) {
e.preventDefault();
$(this).closest('.clone-needs-analysis').clone().insertAfter('.cloneneedsanalysis:last');
$(this).closest('.action-input-plus').hide();
$(this).closest('.action-input-remove').removeClass('hidden');
$('.select2').select2();
});
Use $(this).closest('.clone-needs-analysis').clone(true, true)
instead of $(this).closest('.clone-needs-analysis').clone()
. 使用
$(this).closest('.clone-needs-analysis').clone(true, true)
而不是$(this).closest('.clone-needs-analysis').clone()
。
You need to specify the true, true
parameters to tell JQuery to also clone the attached events. 您需要指定
true, true
参数以告知JQuery还克隆附加事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.