简体   繁体   中英

How to jQuery autocomplete with JSON?

For below JSON

{
  "partnerNameListBeanStruts2Map": [
    {
      "firstName": "sachin",
      "partnerId": 123
    },
    {
      "firstName": "Ankit",
      "partnerId": 234
    }
  ]
}

What code should I wright to done jQuery autocompleter.

Here is my code.

Here I want autocomplete element's value is like sachin OR ankit and id is like like 123 OR 234 is id of element.

$(document).ready(function() {
$(function() {
        $("#search").autocomplete({
        source : function(request, response) {
                $.ajax({
                        url : "list.action",
                        type : "POST",
                        data : {
                            term : request.term
                        },
                        dataType : "json",
                        success : function(data) 
                        {
                        ****What should I write here to work my code?****
                        }
                });
                }
        });
});

According to the doc , You should return your data with the response callback function.

A response callback, which expects a single argument: the data to suggest to the user. This data should be filtered based on the provided term, and can be in any of the formats described above for simple local data. It's important when providing a custom source callback to handle errors during the request. You must always call the response callback even if you encounter an error. This ensures that the widget always has the correct state.

$(function($) {
        $("#search").autocomplete({
        source : function(request, response) {
                $.ajax({
                        url : "list.action",
                        type : "POST",
                        data : {
                            term : request.term
                        },
                        dataType : "json",
                        success : function(data) 
                        {
                            ***response (data) ;***
                        }
                });
                }
        });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM