有什么方法可以检查元素是否已将jquery select2应用于该元素? [英] Is there any way to check if an element has jquery select2 already applied to it?

查看:420
本文介绍了有什么方法可以检查元素是否已将jquery select2应用于该元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to apply select2 to 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

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

所以我想要类似的东西

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

这样的东西存在吗?

推荐答案

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

you can check if the element has select2 attribute

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

编辑

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

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

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

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

这篇关于有什么方法可以检查元素是否已将jquery select2应用于该元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆