如何使用不同风格的 .select2: filter 选项作为不在下拉列表中的第一行?

How to use a different style of .select2: filter option as first line not in dropdown?

而不是单击 select2 表单输入,它会显示一个带有文本框的下拉菜单:

我们如何在文本框替换占位符文本的位置显示它,如下所示:

_form.html.erb

<%= f.grouped_collection_select(:name, @collection, :last, :first, :to_s, :to_s, include_blank: true ) %>

<script>
  $('#lifetime_name').select2({
    placeholder: "Enter Challenge",
    theme: "bootstrap",
    tags: true,
    multiple: false,
  });
</script>

Select2 Github

您在这里实际期望的是该示例中的 multi-select 或 multi-value 下拉框。不是单值下拉框

作为解决方法。试试下面这个。

 $('#lifetime_name').select2({
    placeholder: "Enter Challenge",
    theme: "bootstrap",
    tags: true,
    multiple: true,
  }).on('select2:select', function (e) {
        //clear the input box after a selection is made
        $(this).val([]).trigger('change');
        $(this).val([e.params.data.id]).trigger("change");
    });

参考这个问题: Select2 start with input field instead of dropdown