如何使用来源:函数()...和AJAX的JQuery用户界面自动完成 [英] How to use source: function()... and AJAX in JQuery UI autocomplete

查看:108
本文介绍了如何使用来源:函数()...和AJAX的JQuery用户界面自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一点点的帮助与jQuery UI自动完成。我希望我的文本框( .suggest用户)显示名称从AJAX请求。这是我有:

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
});

任何帮助是非常AP preciated。

Any help is very much appreciated.

推荐答案

在你的AJAX回调,你需要调用响应功能;传递包含要显示的项目的数组。

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
});

如果响应JSON不匹配的jQuery UI自动完成接受,那么你必须将它传递给响应回调之前改造AJAX回调内部结果的格式。 看到这一问题,并接受的答案

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.

这篇关于如何使用来源:函数()...和AJAX的JQuery用户界面自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆