I need a little bit help with JQuery UI Autocomplete. I want my textfield (.suggest-user) display names from an AJAX request. This is what I have:

jQuery("input.suggest-user").autocomplete({
    source : function(request, response) {
        var name = jQuery("input.suggest-user").val();
        jQuery.get("usernames.action?query=" + name, function(data) {
            console.log(data);  // Ok, I get the data. Data looks like that:
            test = data;        // ["one@abc.de", "onf@abc.de","ong@abc.de"]
            return test;        // But what now? How do I display my data?
        });
    },
    minLength : 3
});

非常感谢您的帮助.

推荐答案

Inside your AJAX callback you need to call the response function; passing the array that contains items to display.

jQuery("input.suggest-user").autocomplete({
    source: function (request, response) {
        jQuery.get("usernames.action", {
            query: request.term
        }, function (data) {
            // assuming data is a JavaScript array such as
            // ["one@abc.de", "onf@abc.de","ong@abc.de"]
            // and not a string
            response(data);
        });
    },
    minLength: 3
});

If the response JSON does not match the formats accepted by jQuery UI autocomplete then you must transform the result inside the AJAX callback before passing it to the response callback. See this question and the accepted answer.

Jquery相关问答推荐

在shiny 的datatable列中启用智能搜索

JQuery DateTimePicker-在 Select 中抓取星期几

退出 jquery.each 循环后

自动完成时触发的任何事件?

JQuery通过类名获取所有元素

jQuery $(document).ready () 触发两次

jQuery ajax 调用默认超时值

如何在循环内创建动态变量名称?

jQuery 中的 $this 与 $(this)

组合数组时无法读取未定义的属性推送

为什么 jquery fadeIn() 不能与 .html() 一起使用?

如何在不导致页面滚动的情况下删除位置哈希?

Rails,单击link_to helper后未加载javascript

scrollIntoView 是否适用于所有浏览器?

从父 node 中删除所有子 node ?

在jQuery中将逗号添加到数字

如何将文本从 div 复制到剪贴板

在jquery中查找具有某个属性值的所有元素

CSS3 相当于 jQuery slideUp 和 slideDown?

清除表jquery