首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery表单转发器和select2不能一起使用

jQuery表单转发器和select2不能一起使用
EN

Stack Overflow用户
提问于 2016-12-21 13:41:52
回答 11查看 9.2K关注 0票数 2

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

我已经在google/so上搜索了2天,但似乎不能正常工作。

代码语言:javascript
复制
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);
    }
});

问题是,当select2已经初始化并且已经更改了DOM时,jQuery.repeater克隆了输入和选择元素所在的div标记,因此jQuery.repeater复制了更改后的DOM。我试图在调用repeat操作之前销毁select2,但这也不起作用。

EN

回答 11

Stack Overflow用户

发布于 2016-12-28 17:21:11

我正在做一个项目,在这个项目中,我使用jQuery.repeater重复多个select2输入。下面的方法帮助解决了我在加载输入后初始化输入的问题。

代码语言:javascript
复制
$('.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);
    }
  }
});

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

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

诚挚的问候

票数 13
EN

Stack Overflow用户

发布于 2016-12-28 13:27:45

尝尝这个

代码语言:javascript
复制
include jquery/select2.js/jquery.repeater.js

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

form.repeater({
    show: function () {
    $(this).show(function(){
        // you have not really created this second one
        // so the destroy does not work.
        // Since it is just a copy of the html,
        form.find('select').next('.select2-container').remove();
        form.find('select').select2();
    });
    },
    hide: function (remove) {
      $(this).hide(remove);
    }
});
票数 1
EN

Stack Overflow用户

发布于 2017-12-10 12:59:47

这是一个在表单中继器按钮时钟上初始化select2的解决方案。

代码语言:javascript
复制
<script type="text/javascript">

 

    $("p").mouseover(function(){

        setTimeout(function(){

 

            $(".select2").select2({

                placeholder: "Select a state",

                allowClear: true

            }); 

                  

        }, 100);

    }); 

 

</script>

有关问题和解决方案的深入概述,请参阅see here

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41255897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档