我不知道它是否与新库等冲突,但我无法格式化选择项。他们不会格式化为斜体和粗体。
http://jsfiddle.net/fcb960xh/2/
<select style="width: 300px">
<optgroup label="Alaskan/Hawaiian Time Zone">
<option value="AK">Alaska</option>
<option value="HI">Hawaii</option>
</optgroup>
<optgroup label="Pacific Time Zone">
<option value="CA">California</option>
<option value="NV">Nevada</option>
<option value="OR">Oregon</option>
<option value="WA">Washington</option>
</optgroup>
</select>
JS
$(document).ready(function() {
$('select').select2({
// Specify format function for dropdown item
formatResult: formatResult,
// Specify format function for selected item
formatSelection: formatSelection
});
});
function formatResult(item) {
if(!item.id) {
return item.text;
}
// return item template
return '<i style="color:#ff0000">' + item.text + '</i>';
}
function formatSelection(item) {
// return selection template
return '<b>' + item.text + '</b>';
}
答案 0 :(得分:3)
您正在使用select2 4.0.0。事情已经从以前的版本发生了变化。
来自select2 4.0.0 announcements:
Select2以前提供了多个选项,用于格式化结果列表和所选选项,通常称为“格式化程序”,使用formatSelection和formatResult选项。由于“格式化程序”也用于诸如本地化之类的事情,它也已经改变,它们已经被重命名为templateSelection和templateResult,它们的签名也发生了变化。
您应该使用templateResult
和templateSelection
代替formatResult
和formatSelection
。您还应该返回一个jQuery对象。这是更新后的Fiddle。
答案 1 :(得分:0)
我没有使用过select2,但是快速的方法可能只是为现有的css类添加样式
.select2-results__group{
font-style: italic
}