最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I disable the select2 plugin for certain select dropdowns? - Stack Overflow

programmeradmin1浏览0评论

I'm using the jquery select2 plugin but I want to disable it for certain select dropdowns. Is there a way to exclude select elements the plugin will be applied to? I was thinking maybe a class on the select element to tell the plugin to ignore it.

I'm using the jquery select2 plugin but I want to disable it for certain select dropdowns. Is there a way to exclude select elements the plugin will be applied to? I was thinking maybe a class on the select element to tell the plugin to ignore it.

Share Improve this question asked May 1, 2015 at 15:36 steven35steven35 4,0176 gold badges39 silver badges49 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6
$('select').not('.yourExcludeClass').select2();

Here's a link to the docs for $.not()

While just not calling .select2() on the elements which you don't want to be dropdown works, you might want to disable Select2 after you've already initialized it.

In order to do this, you have two options

  • Visually disable it like you would a standard <select> by calling $("select").prop("disabled", true).
  • "Destroy" Select2 so it goes back to looking like a standard dropdown by calling $("select").select2("destroy").

You can disable it using -

$('select').select2("enable",false);

using JQuery.

I am assuming you are applying it to all selects via:

<script type="text/javascript">
  $('select').select2();
</script>

There are a few ways you could solve it, but the easiest would be to add a class to the selects you want to be styled And only call the select2() on those:

  • Add the class addStyling to your selects to be styled
  • Initialize them with $('select.addStyling').select2();
发布评论

评论列表(0)

  1. 暂无评论