jQuery表单转发器和select2不能一起工作

use*_*314 1 forms jquery repeater jquery-select2 jquery.repeater

我正在使用Select2和jQuery表单转发器(https://github.com/DubFriend/jquery.repeater)

我已经在google /上搜索了2天,但似乎无法让它工作.

include jquery/select2.js/jquery.repeater.js

var form = $('#form');
form.find('select').select2();

form.repeater({
    show: function () {
    $(this).show(function(){
        form.find('select').select2('destroy').select2();
    });
    },
    hide: function (remove) {
      $(this).hide(remove);
    }
});
Run Code Online (Sandbox Code Playgroud)

问题是jQuery.repeater克隆了div标签,其中input2已经初始化并且已经更改了DOM,因此jQuery.repeater会复制更改的DOM.我试图在调用重复动作之前销毁select2,但是dindt也可以工作.

小智 8

我正在开发一个项目,我使用jQuery.repeater重复多个select2输入.以下方法有助于解决我在加载后初始化输入的问题.

$('.job_repeater').repeater({
  show: function () {
    $(this).slideDown();
    $('.select2-container').remove();
    $('Replace with your select identifier id,class,etc.').select2({
      placeholder: "Placeholder text",
      allowClear: true
    });
    $('.select2-container').css('width','100%');
  },
  hide: function (remove) {
    if(confirm('Confirm Question')) {
      $(this).slideUp(remove);
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

更精确的jQuery选择器将有助于仅删除/初始化您想要的选择.

在初始化select2之后我总是使用CSS行来调整父div /容器的宽度.

最好的祝福