如何从 dom 中删除 select2

How to remove select2 from dom

本文关键字:删除 select2 dom      更新时间:2023-09-26

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

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

发起方式如下:

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

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

像这样:

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

会很好。

您需要

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

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

我有用!

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

版本 4.0

$element.data('select2').destroy();
$(".select2").select2('destroy');
$(".select2").val('Your Value');
$('.select2').each(function () {
    $(this).select2({ dropdownParent: $(this).parent() });
});