当您在下拉菜单中选择特定项目时,自定义选择将被隐藏。为什么?在我的选项中,我有closeOnSelect: false;
警告!我使用$.fn.select2.amd.require。 'select2/dropdown/closeOnSelect'无法删除。

$(function() {
  $.fn.select2.amd.require([
      "select2/utils",
      "select2/dropdown",
      "select2/dropdown/attachContainer",
      'select2/dropdown/closeOnSelect'
  ], function (Utils, DropdownAdapter, AttachContainer, closeOnSelect) {
      $('select').select2({
          dropdownAdapter: Utils.Decorate(Utils.Decorate(DropdownAdapter, AttachContainer), closeOnSelect),
          closeOnSelect: false
      });
  });
});
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.12/js/select2.full.js"></script>
<div class="select i-select">
  <select name="" id="">
    <option value="1">White</option>
    <option value="2">Gray light</option>
    <option value="3">Gray</option>
    <option value="4">Black</option>
  </select>
</div>

最佳答案

您可以将closeOnSelect设置为false,然后将其与以下代码一起使用

closeOnSelect= function(){return false;};

$(function() {
  $.fn.select2.amd.require([
      "select2/utils",
      "select2/dropdown",
      "select2/dropdown/attachContainer",
      'select2/dropdown/closeOnSelect'
  ], function (Utils, DropdownAdapter, AttachContainer, closeOnSelect) {

      closeOnSelect= function(){return false;};

      $('select').select2({
          dropdownAdapter: Utils.Decorate(Utils.Decorate(DropdownAdapter, AttachContainer), closeOnSelect)
      });
  });
});
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.12/js/select2.full.js"></script>
<div class="select i-select">
  <select name="" id="">
    <option value="1">White</option>
    <option value="2">Gray light</option>
    <option value="3">Gray</option>
    <option value="4">Black</option>
  </select>
</div>

09-30 17:17