【问题标题】:Add search text in jQuery autocomplete在 jQuery 自动完成中添加搜索文本
【发布时间】:2015-03-29 10:41:10
【问题描述】:

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

我查看了搜索和打开方法,但我无法完全掌握它。基本上我想在源代码中添加搜索文本,然后再次绑定下拉列表。这里是fiddle

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

【问题讨论】:

    标签: jquery autocomplete


    【解决方案1】:

    我不确定这是否是您要查找的内容,但在 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

    【讨论】:

    • 谢谢这是我正在寻找的,但只需要在搜索文本旁边添加新文本
    • 你到底是什么意思?您希望数组在附加新值后按字母顺序排序?
    • 查看我在问题中附加的屏幕截图。您在 aTags 数组中插入的输入值。我想把它作为一个新的价值展示给用户
    猜你喜欢
    • 2012-07-31
    • 2019-05-05
    • 2018-07-29
    • 2012-03-20
    • 2015-09-16
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    相关资源
    最近更新 更多