在 jQuery 自动完成中添加搜索文本

Add search text in jQuery autocomplete

我有数组列表,当用户输入时我必须显示它。我使用了 jQuery 自动完成插件来达到目的,但我必须将搜索文本显示为自动完成列表中的新选项。例如

我看过搜索和打开方法,但我不太明白。基本上我想在源代码中添加搜索文本,然后再次绑定下拉列表。这里是fiddle

$( "#tags" ).autocomplete({
    source: aTags, 
    search:function(){
    },
    open:function(){
    }
});

我不确定这是否是您要查找的内容,但在 search 事件方法中您可以使用新值更新数组,并且 reset the source option, after the plugin has been initialized(查看底部部分)。

var aTags = ["ask", "always", "all", "alright", "one", "foo", "blackberry", "tweet", "force9", "westerners", "sport"],
    // store array length:
    len = aTags.length,
    // cache the element:
    tagsElem = $("#tags");

tagsElem.autocomplete({
    source : aTags,
    search : function( event, ui ) {
        // update array (replace/append value at the end of the array) :
        aTags[len] = tagsElem.val();
        // reset autocomplete source:
        tagsElem.autocomplete( "option", "source", aTags);
    },
    // EDIT:
    open: function( event, ui ) {
        // append "new" text to the last option:
        $('.ui-autocomplete li:last a').append(' <span style="color:red;">new</span>');
    }

});

JSFiddle JSFiddle