我正在使用Select2进行标签设置,我已经设置好用户可以添加新标签。我面临的问题是验证用户输入并将经过处理的标签添加到选择中。
更具体地说,当用户在标签中输入空格时,我使用formatNoMatches显示js链接以清理标签,然后通过编程方式添加标签。这段代码似乎没有错误,但是当调用sanitize时,输入的所有选择都被清除了。
有任何线索告诉我哪里出错了吗?
更具体地说,当用户在标签中输入空格时,我使用formatNoMatches显示js链接以清理标签,然后通过编程方式添加标签。这段代码似乎没有错误,但是当调用sanitize时,输入的所有选择都被清除了。
有任何线索告诉我哪里出错了吗?
var data=[{id:0,tag:'enhancement'},{id:1,tag:'bug'},{id:2,tag:'duplicate'},{id:3,tag:'invalid'},{id:4,tag:'wontfix'}];
function format(item) { return item.tag; }
function sanitize(a){
$("#select").select2('val',[{
id: -1,
tag: a
}]);
console.log(a);
};
$("#select").select2({
tags: true,
// tokenSeparators: [",", " "],
createSearchChoice: function(term, data) {
return term.indexOf(' ') >= 0 ? null :
{
id: term,
tag: term
};
},
multiple: true,
data:{ results: data, text: function(item) { return item.tag; } }, formatSelection: format, formatResult: format,
formatNoMatches: function(term) { return "\"" + term + "\" <b>Is Invalid.</b> <a onclick=\"sanitize('"+ term +"')\">Clear Invalid Charecters</a>" }
});