javascript - JQuery Autocomplete with URL(Provides JSON) -
i have autocomplete box (below) having source php file.
$("#autocompletefrom").autocomplete({ source: "../wp-content/plugins/plugin-search/js/getcountry.php", select: function(event, ui) { $("#autocompletefromhidden").val(ui.item.code); $("#autocompletenamefromhidden").val(ui.item.name); } });
now want change source of autocomplete url returns list of locations in json format below.
{"currentculture":null,"airports":[{"code":"bcn","name":"el prat airport"}, {"code":"cdg","name":"paris-charles de gaulle"},{"code":"ory","name":"paris- orly"}],"destinations":null}
please me on this. let me if need provide more details.
adding label attribute json items might solve you're problem. see if helps:
$( function() { $( "#autocompletefrom" ).autocomplete({ source: function( request, response ) { var search_data = []; var data = {"currentculture":null,"airports":[{"code":"bcn","name":"el prat airport"}, {"code":"cdg","name":"paris-charles de gaulle"},{"code":"ory","name":"paris-orly"}],"destinations":null}; data.airports.map(function(item){ item.label = item.name; search_data.push(item); }); response(search_data); }, minlength: 2, select: function( event, ui ) { $("#autocompletefromhidden").val(ui.item.code); $("#autocompletenamefromhidden").val(ui.item.name); } } ); } );
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <div class="ui-widget"> <label for="countries">countries: </label> <input id="autocompletefrom"> </div> result: <div class="ui-widget"> <input type="text" id="autocompletefromhidden" class="ui-widget-content"/> <input type="text" id="autocompletenamefromhidden" class="ui-widget-content"/> </div>
Comments
Post a Comment