如何在无法输入文本的情况下使用 Select2 中的 'multiple select boxes' 选项?

How can I use 'multiple select boxes' option in Select2 without ability to enter text?

当您使用 select2 'Multiple select boxes' 选项时,它会为您提供输入文本的选项。我想删除此功能,以便用户只能 click/touch 可用的选项,文本光标不会显示。

您可以在他们的示例页面上看到 'Multiple select boxes' 选项:https://select2.github.io/examples.html

我的代码。 (我正在使用 Laravel 5 blade 模板)

  <div class="row searchBoxes show">
    {!! Form::open(['method' => 'GET', 'id' => 'searchForm', 'name' => 'searchForm', 'route' => 'articles_path']) !!}
        {!! Form::select('categoryList[]', $categories, null, ['class' => 'categorySelector', 'id' => 'categoryList', 'multiple']) !!}
        {!! Form::select('dayList[]', $days, null, ['class' => 'distanceSelector', 'id' => 'dayList', 'multiple']) !!}
    {!! Form::submit('MAKE ME HAPPY', array('id' => 'submitButton', 'class' => 'searchSubmit')) !!}
   {!! Form::close() !!}
</div>

    <script type="text/javascript">
    $('#categoryList').select2({
            placeholder: "CATEGORY",
            minimumResultsForSearch: -1
          });
      $('#dayList').select2({
        placeholder: "DAY",
      minimumResultsForSearch: -1
      });
    </script>

编辑: 我找到了关于 'Hiding the search box'. I have tried switching minimumResultsForSearch to 'infinity' and it still didn't work. Here is my app: www.gethappy.co.nz

的信息

使用 属性 minimumResultsForSearch 并将其设置为 -1

$('#categoryList').select2({
    placeholder: "CATEGORY",
    minimumResultsForSearch: -1
});

Example

已列出问题 here

我最后只是编辑了 select2.js 文件的代码。

我更改了这部分(大约 1810 行)

  Search.prototype.render = function (decorated) {
    var $search = $(
      '<li class="select2-search select2-search--inline">' +
        '<input class="select2-search__field" type="search" tabindex="-1"' +
        ' autocomplete="off" autocorrect="off" autocapitalize="off"' +
        ' spellcheck="false" role="textbox" aria-autocomplete="list" />' +
      '</li>'
    );

为此:

  Search.prototype.render = function (decorated) {
    var $search = $(
      '<li>' +
        '<input tabindex="-1"' +
        ' autocomplete="off" autocorrect="off" autocapitalize="off"' +
        ' spellcheck="false" role="textbox" aria-autocomplete="list" style="display: none" />' +
        '<p class="placeHolderText"></p>' +
      '</li>'
    );

我不得不在其中添加一个新的占位符,因为它摆脱了 select2 占位符。然后我用 jquery.

添加了占位符文本

还有其他答案HERE,但它们对我不起作用。