有没有办法检查一个元素是否已经应用了 jquery select2?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29854022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:38:30  来源:igfitidea点击:

Is there any way to check if an element has jquery select2 already applied to it?

jqueryjquery-select2

提问by leora

I want to apply select2to a bunch of jquery elements on the page that all have the same class name but it looks like if i call select2() on an element that already has had a select2() called on it then it blows up. here is my code

我想将select2应用于页面上的一堆 jquery 元素,这些元素都具有相同的类名,但看起来如果我在一个已经调用了 select2() 的元素上调用 select2() 那么它就会爆炸。这是我的代码

 $('.MyDropdowns').each(function (i, obj) {
    $(obj).select2({ width: "455px" });
});

so I want something like:

所以我想要这样的东西:

 $('.MyDripdowns').each(function (i, obj) {
    if (!$(obj).HasSelect2Initiatized)
    {
        $(obj).select2({ width: "455px" });
    }
});

Does anything like this exist?

这样的东西存在吗?

回答by semirturgay

you can check if the element has select2attribute

您可以检查元素是否具有select2属性

$('.MyDripdowns').each(function (i, obj) {
    if (!$(obj).data('select2'))
    {
        $(obj).select2({ width: "455px" });
    }
});

EDIT

编辑

As @Fr0zenFyr said in his comment for v4.0 you can use :

正如@Fr0zenFyr 在他对 v4.0 的评论中所说,您可以使用:

if (!$(obj).hasClass("select2-hidden-accessible"))

if (!$(obj).hasClass("select2-hidden-accessible"))

回答by userlond

Working solution:

工作解决方案:

$('.MyDripdowns:not([class^="select2"])').each(function (i, obj) {
    $(obj).select2({width: "455px"});
})

Links:

链接:

  1. ^= attribute starts with selector
  2. :not selector
  1. ^= 属性以选择器开头
  2. :不是选择器

回答by Daxesh Vora

Above answer is almost correct.
But it creates problem when we are adding elements dynamically on same page and applying select 2 to newly created element.
At that times selector has to be specified using not only class but also with input type. PFB reference code.

上面的答案几乎是正确的。
但是当我们在同一页面上动态添加元素并将选择 2 应用于新创建的元素时,它会产生问题。
那时,选择器不仅要使用类,还要使用输入类型来指定。PFB 参考代码。

$('inputp[type="text"].MyDripdowns').each(curr_idx, curr_elem){
    //Check if select 2 is already applied or not
    if($(curr_elem).hasClass('.select2-offscreen')){
      //Select 2 is already applied to this element
    }
    else{
       //Apply Select 2 to this element 
    }
}

回答by lofihelsinki

You can check if Select2 operations give and error or not with try..catch. If there is an error thrown, that means that there is not Select2 in the element.

您可以检查 Select2 操作是否给出和错误try..catch。如果抛出错误,则表示元素中没有 Select2。

The downside is that this will still output an error in the browser console.

缺点是这仍然会在浏览器控制台中输出错误。

try {
    $(obj).select2("close")
} catch(err) {
    // No Select2 in the element
    $(obj).select2({ width: "455px" });
}