简体   繁体   English

在同一页面/站点上选择2个多个版本

[英]select2 multiple versions on same page/site

Afternoon, 下午,

On a website I already updated lots of elements that used select2 v. 3.5.2 to the new v. 4.0 (and some of those elements are on the header of the website that is present on ALL pages) 在一个网站上,我已经将许多使用select2 v.3.5.2的元素更新为新的v.4.0(其中一些元素在所有页面上显示的网站标题中)

Unfortunately on some pages of the website the X-editable jquery plugin is used, and that plugin doesn't play well with v 4.0 of select2 (please read: does not play at all) 不幸的是,在网站的某些页面上使用了X可编辑的jquery插件,并且该插件不能与select2 v 4.0一起很好地使用(请阅读:完全不起作用)

My question is: Can I use both versions of select2 on some pages? 我的问题是:在某些页面上可以同时使用两个版本的select2吗?

And if so, how? 如果是这样,怎么办? Since $(...).select2(); 由于$(...).select2(); is blind to which version of select2 is loaded.... 不知道加载哪个版本的select2。...

Example code: 示例代码:

    $("#search_species").select2({
        minimumInputLength: 1,
        maximumSelectionSize: 1,
        ajax: {
            url: "/php/search.php",
            dataType: 'json',
            delay: 250,
            data: function (params) {
                return {
                    query: params.term
                };
            },
            processResults: function (data, params) {
                return { results: data };
            },
            cache: true
        },
        escapeMarkup: function (markup) { return markup; },k

        templateResult: formatarResultados,
        templateSelection: formatarSeleccao,

    }).on("select2:selecting", function(e) {
        console.log(e);

        // window.location.href = e.params.args.data.url;
        // $('.select2-drop').hide();

    });

It should be possible (but not necessarily easy) to isolate Select2 4.0.0. 隔离Select2 4.0.0应该有可能(但不一定很容易)。 It is nearly impossible to isolate older versions because they rely on global variables, while Select2 4.0.0 is actually pretty self-contained (with some exceptions). 隔离较旧的版本几乎是不可能的,因为它们依赖于全局变量,而Select2 4.0.0实际上是完全独立的(有一些例外)。

I'm guessing you are running into a situation similar to the following: 我猜您正在遇到类似于以下情况:

 $(".select2").select2({ ajax: {} }); 
 <link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.css" rel="stylesheet"/> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.css " rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.js"></script> <select class="select2"> <option value="AL">Alabama</option> <option value="AK">Alaska</option> <option value="AZ">Arizona</option> </select> 

You can see both Select2 4.0.0 and 3.5.2 are being loaded in the same bin, and 3.5.2 is winning the battle. 您可以看到Select2 4.0.0和3.5.2都装在同一个纸箱中,而3.5.2赢得了胜利。

But this can be fixed by taking a reference to $.fn.select2 after you load the plugin. 但这可以通过在加载插件后引用$.fn.select2$.fn.select2 You can then either call this function directly (using .call() ) or re-set the reference when you need it. 然后,您可以直接调用此函数(使用.call() ),也可以在需要时重新设置引用。 I would personally recommend calling the function directly, so other plugins aren't breaking because of you. 个人建议直接调用该函数,以便其他插件不会因为您而中断。

 myOwnSelect2.call($(".select2"), { ajax: {} }); $(".select3").select2(); 
 <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.css " rel="stylesheet" /> <script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.js"></script> <script> var myOwnSelect2 = $.fn.select2; delete $.fn.select2; </script> <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.css " rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.js"></script> <select class="select2"> <option value="AL">Alabama</option> <option value="AK">Alaska</option> <option value="AZ">Arizona</option> </select> <select class="select3"> <option value="AL">Alabama</option> <option value="AK">Alaska</option> <option value="AZ">Arizona</option> </select> 

And using the function directly has the benefit of both Select2 3.x and 4.x working on the same page without any problems. 直接使用该功能的好处是Select2 3.x和4.x在同一页面上都可以正常工作。

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

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