我有一个国家/地区的json列表:http://vocab.nic.in/rest.php/country/json
我正在尝试通过猎犬建议引擎获取country_id和国家名称。O尝试了以下代码:
var countries = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('country_name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 10,
prefetch: {
url: 'http://vocab.nic.in/rest.php/country/json',
filter: function(response) {
return response.countries;
}
}
});
$('#my-input').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'states',
displayKey: 'value',
source: countries.ttAdapter()
});这不管用。我应该如何更改代码才能使其正常工作?
发布于 2014-03-29 21:08:26
// instantiate the bloodhound suggestion engine
var countries = new Bloodhound({
datumTokenizer: function(countries) {
return Bloodhound.tokenizers.whitespace(countries.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: "http://vocab.nic.in/rest.php/country/json",
filter: function(response) {
return response.countries;
}
}
});
// initialize the bloodhound suggestion engine
countries.initialize();
// instantiate the typeahead UI
$('.typeahead').typeahead(
{ hint: true,
highlight: true,
minLength: 1
},
{
name: 'countries',
displayKey: function(countries) {
return countries.country.country_name;
},
source: countries.ttAdapter()
});Typeahead输出

备注:
服务器上的预取数据=“
发布于 2019-09-16 22:33:06
注意:如果您执行了所有这些操作,但仍然不起作用,请检查您的data.json文件(无论您如何命名它)
好的格式示例:https://github.com/twitter/typeahead.js/blob/gh-pages/data/countries.json
'data1','data2',.....
我被不合适的引述给绊倒了。
https://stackoverflow.com/questions/22730407
复制相似问题