【发布时间】:2016-08-26 14:56:56
【问题描述】:
我有一个 select2 标记控件。当标签不存在时,用户可以创建标签。此标签保存在数据库中,但问题是我不知道如何设置为 ajax 成功返回的标签 id。
$tags.select2({
placeholder: '- Select -',
tags: true,
tokenSeparators: [',', ' '],
createTag: function (params) {
if (params.term.trim() === '') {
return null;
}
return {
id: '', //How to get id here???
text: params.term.toLowerCase(),
newTag: true // add additional parameters
}
},
//Ajax to get tags here ommited
});
$tags.on('select2:select', function (e) {
if (e.params.data.newTag) {
$.ajax({
url: urlAddTag,
type: 'POST',
dataType: "json",
data: { tag: e.params.data.text.toLowerCase() },
success: function (response) {
e.params.data.id = response.id;
},
error: function (xhr, ajaxOptions, thrownError) {
var msg = JSON.parse(xhr.responseText);
}
});
}
});
在 ajax 响应中,我正在设置标签存储在数据库中的 id,但我不知道如何传递给“createTag”函数。
【问题讨论】:
标签: javascript ajax select2