结合在jQuery用户界面自动完成本地源和远程源用户界面、自动完成、jQuery

2023-09-10 19:20:15 作者:脑细胞怀孕了.

我包括在本地的javascript的常用术语列表,然后我也想从服务器获得通过Ajax响应JSON响应。怎样才能做到呢?

I included locally in javascript a list of commonly used terms, and then I would also like to get json response from the server through ajax response. How can it be done?

var projects = ["apple", "orange"];

$('#search').autocomplete({
    source: projects
});

然后从阿贾克斯追加的结果?

then append the result from ajax?

推荐答案

你会去,这将是结合你从服务器获取与本地结果阵列后面的结果的方式。

The way you would go about this would be to combine the results you get back from the server with the local results array. You can accomplish this by passing a function to the source option of autocomplete:

有三个步骤,你将不得不执行:

There are three steps you'll have to perform:

请AJAX请求,并从服务器获取结果。 筛选本地阵列 联合的结果

这应该是pretty的简单。像这样的工作:

This should be pretty simple. Something like this would work:

$("input").autocomplete({
    source: function(request, response) { 
        /* local results: */
        var localResults = $.ui.autocomplete.filter(localArray, request.term);

        /* Remote results: */
        $.ajax({
            /* AJAX options omitted... */
            success: function(data) {
                /* Process remote data using $.map, if necessary, then concatenate local
                 * and remote results. 
                 */
                response(data.concat(localResults));
            }
        });
    }
}); 

我已经在这里工作了一个完整的例子: http://jsfiddle.net/FZ4N4/