标签数组必须包含具有键id和文本的对象.如果需要,您可以添加更多的键(对于您的情况,我添加了代表该数字的键qt).
要向选项添加HTML,您需要更改默认的formatResult函数.使用以下代码,数字将显示在存在的变量(即传递给select2的变量)中.对于即时创建的选项,数字不会显示.
$(".tag").select2({
tags:[
{id: "red", text: "red", qt: 3},
{id: "green", text: "green", qt: 12},
{id: "blue", text: "blue", qt: 5},
{id: "black", text: "black", qt: 7}
],
formatResult: function(result) {
if (result.qt === undefined) {
return result.text;
}
return result.text
+ ""
+ result.qt
+ "";
}
});