我試圖從ajax數據創建自動完成功能,但它不工作。Typeahead ajax自動完成功能無法正常工作
這是我的代碼。
HTML
<input type="search" class="typeahead" id="order_address" autocomplete="off">
的Javascript
$(文件)。就緒(函數(){
var suggestions = new Bloodhound({ remote: { url: 'https://autocomplete.geocoder.api.here.com/6.2/suggest.json?app_id=...app_code=...&country=USA&mapview=41.382995,-74.301616;41.305715,-74.092278&query=%QUERY%', wildcard: '%QUERY%', filter: function (response) { return response.suggestions; } }, datumTokenizer: function(suggestions) { return Bloodhound.tokenizers.whitespace(suggestions); }, queryTokenizer: Bloodhound.tokenizers.whitespace, }); $('#order_address').typeahead({ hint: true, highlight: true, minLength: 1 }, { name: 'suggestions', displayKey: function(suggestions) { return suggestions.label; }, source: suggestions.ttAdapter() });
});
當我從瀏覽器網絡數據檢查時,它正確地得到如下的建議數據。
{"suggestions":[{"label":"United States Of America, NY, Cornwall, Angola Rd","language":"en","countryCode":"USA","locationId":"NT_7Cpok364jILgH4ksUcyjpC","address":{"country":"United States Of America","state":"NY","county":"Orange","city":"Cornwall","street":"Angola Rd","postalCode":"12518"},"distance":14896,"matchLevel":"street"},{"label":"United States Of America, NY, Garrison, Albany Post Rd","language":"en","countryCode":"USA","locationId":"NT_DM6n2RQmjZ1YvBjMS6MyGA","address":{"country":"United States Of America","state":"NY","county":"Putnam","city":"Garrison","district":"Garrison","street":"Albany Post Rd","postalCode":"10524"},"distance":23981,"matchLevel":"street"},{"label":"United States Of America, NY, Montrose, Albany Post Rd","language":"en","countryCode":"USA","locationId":"NT_NNt..Hu2Z5yXhvu4UpGXwA","address":{"country":"United States Of America","state":"NY","county":"Westchester","city":"Montrose","street":"Albany Post Rd","postalCode":"10548"},"distance":24394,"matchLevel":"street"},{"label":"United States Of America, NY, Croton-on-Hudson, Albany Post Rd","language":"en","countryCode":"USA","locationId":"NT_fokNpGY5GJxSkp195bkloA","address":{"country":"United States Of America","state":"NY","county":"Westchester","city":"Croton-on-Hudson","street":"Albany Post Rd","postalCode":"10520"},"distance":26329,"matchLevel":"street"}]}
但它不工作自動完成。
我現在能做什麼?
謝謝你,@litel。你給了我很大的幫助。 – Alex