我如何改进jQuery;把它贴上“标签”;自动完成?

我如何改进jQuery;把它贴上“标签”;自动完成?,jquery,autocomplete,tags,tag-it,Jquery,Autocomplete,Tags,Tag It,我喜欢jQuery的TagIt插件,但是如果我将它设置为autocomplete,它并不总是按照我想要的方式工作 这里有一个例子 我的自动完成数组由“粉红女士苹果”、“史密斯奶奶苹果”、“金色美味苹果”和“苹果”组成 如果我输入“苹果”,它不会提示粉红女士、史密斯奶奶或金色美味。它只是暗示苹果。有没有一种方法可以改变它,这样它也可以扫描包含Apple但不是以Apple开头的标记?它从wai aria实现继承的标记autocomplete。默认情况下,它只知道三种状态: 无-完全没有完成 内联-以

我喜欢jQuery的TagIt插件,但是如果我将它设置为autocomplete,它并不总是按照我想要的方式工作

这里有一个例子

我的自动完成数组由“粉红女士苹果”、“史密斯奶奶苹果”、“金色美味苹果”和“苹果”组成


如果我输入“苹果”,它不会提示粉红女士、史密斯奶奶或金色美味。它只是暗示苹果。有没有一种方法可以改变它,这样它也可以扫描包含Apple但不是以Apple开头的标记?

它从wai aria实现继承的标记autocomplete。默认情况下,它只知道三种状态:

无-完全没有完成
内联-以…
列表-所有可用

因此,如果不扩展tagit以使用不同的自动完成方法,这是不可能的

有关WAI-ARIA的更多信息,请参见此处:


它使用标记它的属性标记source为我工作。我正在使用

我也面临着同样的问题,所以我使用@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()));
}
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()));
    }
});
我复制了该函数并将其粘贴到.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);
    });
    showChoices(this._subtractArray(choices, this.assignedTags()));
}
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()));
    }
});
示例:

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()));
}
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()));
    }
});
从availableTags切换到tagSource对我也很有用。