当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - Iterate over data to show autocomplete results

问题描述:

I m trying to show Autocomplete results.

Here is the data that my script returns when I query it using a GET request

For example when I query it "fb"

It returns me the following data

 "fb"

"fbi"

"fbi most wanted"

"fbi jobs"

"fb banners"

"fbook"

"fb covers"

"fbanners"

"fbisd"

"fbop"

Here's the code for the Autocomplete plugin

 $("#query").autocomplete({

source: function( request, response ) {

$.ajax({

url: "q.php",

dataType: "json",

data: {

"q" : request.term

},

success: function( data ) {

response(data[1])

}

});

},

minLength: 2

});

It doesnt show me any result. Can anyone tell me where am I going wrong here!

网友答案:
$("#query").autocomplete({
        source: "q.php",
        minLength: 2
    }).data("autocomplete")._renderItem = function (ul, item) {
            return $("<li></li>")
            .append("<a>" + item+ "</a>")
            .appendTo(ul);
        };
网友答案:

If you use the jQuery Autocomplete plugin, why would you over-complicate it and render it by yourself?

Just define a source script and provide a well formated JSON response as the Autocomplete module wants it:

Expected data format

The data from local data, a url or a callback can come in two variants:

An Array of Strings: [ "Choice1", "Choice2" ]

An Array of Objects with label and value properties: [ { label: "Choice1", value: "value1" }, ... ]

source: "query.php";

Let the plugin to do the rest.

Read the API: jQuery Autocomplete plugin overview

If you want to do anything else, just register for and catch an event.

分享给朋友:
您可能感兴趣的文章:
随机阅读: