使用以下HTML代码:
<input type='hidden' id='cantseeme'>
我没有任何问题在动态创建Select2控件方面,也能添加我的选项:
// simplified example
var select2_ary = [];
select2_ary.push({
id : "one",
text : "one"
});
select2_ary.push({
id : "two",
text : "two"
});
$("#cantseeme").select2({
placeholder : "Pick a number",
data : select2_ary
});
然而,我似乎无法弄清楚如何以这种方式添加 optgroups
。我在github上找到了这个讨论和这篇博客文章,但前者似乎没有讨论动态optgroup
的添加,后者我根本就看不懂。
有人有什么想法吗?