javascript 如何从dom中删除select2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19328253/
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-10-27 15:11:29  来源:igfitidea点击:

How to remove select2 from dom

javascriptjquery-select2

提问by jdennison

Say we have a simple select2list:

假设我们有一个简单的select2列表:

<select id="e1">
  <option value="AL">Alabama</option>
    ...
  <option value="WY">Wyoming</option>
</select>

Initiated like:

发起如下:

$("#e1").select2();

How can I remove select2and return it to a regular dropdown? I can't find any examples or entry in documentation.

如何删除select2并将其返回到常规下拉列表?我在文档中找不到任何示例或条目。

Something like:

就像是:

$("#e1").select2('remove'); 

would be nice.

会好的。

回答by PSL

You need to use destroymethod on select2. See the Documentation

您需要destroy在 select2上使用方法。查看文档

i.e

IE

 $("#e1").select2('destroy'); 

回答by jcmordan

That work for me!

这对我有用!

var $select = $('.select2').select2();
//console.log($select);
$select.each(function(i,item){
  //console.log(item);
  $(item).select2("destroy");
});

回答by Nasri Zhang

Version 4.0

4.0版

$element.data('select2').destroy();