【发布时间】:2015-08-05 00:08:14
【问题描述】:
我正在使用最新版本的 select2 (4.0.0),但找不到使 optgroup 可选的选项。
一个 optgroup 用于对下拉列表的不同选项进行分组,如它们的基本示例所示:
我也需要这个 optgoup 是可选的!这在 3.5.1 中是可能的,但它不再是 4.0.0 中的默认设置。
我的代码如下所示:
$(document).ready(function() {
$('#countrySelect').select2({
data: [{
text: "group",
"id": 1,
children: [{
"text": "Test 2",
"id": "2"
}, {
"text": "Test 3",
"id": "3",
"selected": true
}]
}]
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://select2.github.io/dist/js/select2.full.js"></script>
<link href="https://select2.github.io/dist/css/select2.min.css" rel="stylesheet" />
<select id="countrySelect" style="width: 380px;" placeholder="Select regions..."></select>
【问题讨论】:
-
不是我重复的,因为我的问题使用 select2 4.0 版,而标记的问题使用的是以前的版本。那里发布的解决方案对我不起作用。
-
即使它有效,我更喜欢这个答案。它解释了为什么无法选择 optgroups 可选并且不提供“hack”解决方法。
-
好吧,我遇到了这个问题,我找到了这个解决方法enter link description here
标签: jquery-select2