我有以下JSON,通过/tags检索
[
{
"id": "CSS",
"text": "CSS"
},
{
"id": "HTML",
"text": "HTML"
},
{
"id": "JavaScript",
"text": "JavaScript"
},
{
"id": "jQuery",
"text": "jQuery"
},
{
"id": "MySQL",
"text": "MySQL"
},
{
"id": "PHP",
"text": "PHP"
}
]我有一个<input />,它通过使用Select2来接受标记:
<input name="Tags" id="Tags" value="PHP,HTML,jQuery" />我用这种方式附加了Select2:
$("#Tags").select2({
tags: true,
tokenSeparators: [",", " "],
createSearchChoice: function(term, data) {
if ($(data).filter(function() {
return this.text.localeCompare(term) === 0;
}).length === 0) {
return {
id: term,
text: term
};
}
},
multiple: true,
ajax: {
url: '/tags',
dataType: "json",
data: function(term, page) {
return {
q: term
};
},
results: function(data, page) {
return {
results: data
};
}
}
});问题
/tags发出请求,但它不加载标记。控制台中也没有错误。我使用的是来自CDN的Select2 3.5.2。我哪里出问题了?
发布于 2015-04-11 19:26:57
我用过这个。看上去像是黑客或是工作人员。
$("#Tags").select2({
tags: true,
multiple: true
});
$.getJSON("/tags", function (data) {
if (data.length > 0)
$("#Tags").select2({
tags: data,
multiple: true
});
});希望这对某人有帮助。在我得到更好的建议之前,我会保持开放的态度。
https://stackoverflow.com/questions/29580162
复制相似问题