我喜欢jQuery的Tag-It插件,但是如果我将它设置为自动完成,它并不总是以我想要的方式工作。
下面是一个例子。
我的自动完成数组由“粉红女士苹果”、“祖母史密斯苹果”、“金色美味苹果”和“苹果”组成。
如果我输入“苹果”,它不会显示“粉红女士”、“史密斯奶奶”或“金色美味”。它只表明是苹果公司。有没有一种方法可以让它也扫描包含Apple但不是以Apple开头的标签?
发布于 2012-02-22 07:08:59
我也面临着同样的问题,所以我使用了@Ravindra的技巧(+1btw)来查看是否可以对插件进行反向工程,并弄清楚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标记初始化函数的参数之一包含在内。然后,我将其修改为使用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()));
}
});发布于 2012-01-27 21:12:27
它使用tagSource的Tag- It 属性为我工作。我正在使用http://aehlke.github.com/tag-it/
发布于 2011-07-27 20:30:00
Tag-It继承了wai-aria实现的autocomplete。默认情况下,它只知道三种状态:
None -根本没有完成
内联-以...开头
列表-所有可用
因此,如果不扩展tag-it来使用不同的自动完成方法,这是不可能的。
有关WAI-ARIA的更多信息,请参阅此处:
http://www.w3.org/WAI/intro/aria
http://test.cita.illinois.edu/aria/
https://stackoverflow.com/questions/6844085
复制相似问题