Javascript 如何显示optgroup标签+;jQuery select2中的值
我正在使用一个名为的jQuery插件,其中我使用了Javascript 如何显示optgroup标签+;jQuery select2中的值,javascript,jquery,jquery-select2,Javascript,Jquery,Jquery Select2,我正在使用一个名为的jQuery插件,其中我使用了Multiselect。以前我还在我的另一个网站上使用相同的插件,但现在这个插件已经很旧了。我想,当我从下拉列表中选择选项时,会显示optgroup,如下所示: 现在我正在使用的这个插件向我展示如下: 我希望optgroup标记在选择时具有值。 以下是select2的工作演示: 以前我使用此脚本显示带有值的optgroup: function format(item) { opt = $(item.element); sel = opt.
Multiselect
。以前我还在我的另一个网站上使用相同的插件,但现在这个插件已经很旧了。我想,当我从下拉列表中选择选项时,会显示optgroup
,如下所示:
现在我正在使用的这个插件向我展示如下:
我希望optgroup标记在选择时具有值。
以下是select2
的工作演示:
以前我使用此脚本显示带有值的optgroup:
function format(item) {
opt = $(item.element);
sel = opt.text();
og = opt.closest('optgroup').attr('label');
return og+' | '+item.text;
}
$("select").select2({
formatSelection: format,
escapeMarkup: function(m) { return m; }
});
$(".select2_sample3").select2({
tags: []
});
使用
模板选择
这是基于您的代码的工作示例
function formatState (item) {
opt = $(item.element);
og = opt.closest('optgroup').attr('label');
return og+' | '+item.text;
};
$('#example').select2({
placeholder: 'Select a Category',
templateSelection: formatState
});
jsfiddle:
参考资料:是的,你说得对,对我来说非常好。非常感谢。