【发布时间】:2015-04-27 08:57:30
【问题描述】:
我们使用选择二要标记的输入。我们需要触发自动保存只要选择一个标签。有当选择一个新的或现有的标记用于选择二回调? P>
$(".tagstypeahead").select2({
tags: true,
tokenSeparators: [','],
createSearchChoice: function (term) {
return {
id: $.trim(term),
text: $.trim(term) // + ' (new tag)' - Adds new tag to the end here
};
},
ajax: {
url: '/tags/typeahead.json',
dataType: 'json',
data: function(term, page) {
return {
q: term
};
},
results: function(data, page) {
return {
results: data
};
}
},
// Take default tags from the input value
initSelection: function (element, callback) {
var data = [];
function splitVal(string, separator) {
var val, i, l;
if (string === null || string.length < 1) return [];
val = string.split(separator);
for (i = 0, l = val.length; i < l; i = i + 1) val[i] = $.trim(val[i]);
return val;
}
$(splitVal(element.val(), ",")).each(function () {
data.push({
id: this,
text: this
});
});
callback(data);
}
});
【问题讨论】:
标签: javascript jquery ajax jquery-select2