Html:
<input type="text" name="country" id="autocomplete"/>
Ajax 查找
$('#autocomplete').autocomplete({ serviceUrl: '/autocomplete/countries', onSelect: function (suggestion) { alert('You selected: ' + suggestion.value + ', ' + suggestion.data); } });
本地查找 (没有 Ajax):
var countries = [ { value: 'Andorra', data: 'AD' }, // ... { value: 'Zimbabwe', data: 'ZZ' } ]; $('#autocomplete').autocomplete({ lookup: countries, onSelect: function (suggestion) { alert('You selected: ' + suggestion.value + ', ' + suggestion.data); } });
自定义查找函数
$('#autocomplete').autocomplete({ lookup: function (query, done) { // Do Ajax call or lookup locally, when done, // call the callback and pass your results: var result = { suggestions: [ { "value": "United Arab Emirates", "data": "AE" }, { "value": "United Kingdom", "data": "UK" }, { "value": "United States", "data": "US" } ] }; done(result); }, onSelect: function (suggestion) { alert('You selected: ' + suggestion.value + ', ' + suggestion.data); } });