我遇到了同样的问题,所以我使用@Ravindra 的提示(+1 BTW)来查看我是否可以对插件进行逆向工程并找出 tagSource 函数预期返回的内容。
tagSource 函数返回一个布尔值。如果 availableTags 数组中的标签显示在自动完成列表中,则返回 True。返回 False 表示不应显示该标签。
这是默认的 tagSource 函数,它使用 indexOf 来确定到目前为止键入的文本是否与availableTags 数组中的标记开头匹配:
原始,默认功能:
tagSource: function(search, showChoices) {
var filter = search.term.toLowerCase();
var choices = $.grep(this.options.availableTags, function(element) {
// Only match autocomplete options that begin with the search term.
// (Case insensitive.)
return (element.toLowerCase().indexOf(filter) === 0);
});
showChoices(this._subtractArray(choices, this.assignedTags()));
}
我复制了该函数并将其粘贴到 .tagit 函数中,因此它被包含为传递给 jQuery tagit 初始化函数的参数之一。然后我将其修改为使用 match 方法,该方法使用模式匹配来返回与模式匹配的字符串部分。如果匹配返回 null,则不要在列表中显示它。如果它返回任何其他内容,请在列表中显示该标签:
作为参数传入的修改函数:
tagSource: function(search, showChoices) {
var filter = search.term.toLowerCase();
var choices = $.grep(this.options.availableTags, function(element) {
// Only match autocomplete options that begin with the search term.
// (Case insensitive.)
//return (element.toLowerCase().indexOf(filter) === 0);
console.info(element.toLowerCase().match(filter) + " : " + element + " : " + filter);
return (element.toLowerCase().match(filter) !== null);
});
showChoices(this._subtractArray(choices, this.assignedTags()));
}
例子:
$('#tagged').tagit({
onTagRemoved: function() {
alert("Removed tag");
},
availableTags: [ "one" , "two one" , "three" , "four" , "five" ],
// override function to modify autocomplete behavior
tagSource: function(search, showChoices) {
var filter = search.term.toLowerCase();
var choices = $.grep(this.options.availableTags, function(element) {
// Only match autocomplete options that begin with the search term.
// (Case insensitive.)
//return (element.toLowerCase().indexOf(filter) === 0);
console.info(element.toLowerCase().match(filter) + " : " + element + " : " + filter);
return (element.toLowerCase().match(filter) !== null);
});
showChoices(this._subtractArray(choices, this.assignedTags()));
}
});