【发布时间】:2015-03-11 19:07:38
【问题描述】:
我有一个这样的 json:
{"num": ["test", "test group event"]}
我想用typeahead.js, 但是他们的json是这样的:
["Andorra","United Arab Emirates"]
如何在下面的示例中使用我的 json?
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
var countries = new Bloodhound({
datumTokenizer : Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer : Bloodhound.tokenizers.whitespace,
limit : 10,
prefetch : {
url : 'http://127.0.0.1:8000/mysearch/',
filter : function(list) {
return $.map(list, function(country){
return {
name : country
};
});
}
}
});
countries.initialize();
$('#prefetch .typeahead').typeahead(null, {
name : 'countries',
displayKey : 'name',
source : countries.ttAdapter()
});
});//]]>
</script>
更新:
我的 json 来自服务器(http://rroyales.webfactional.com/mysearch/1.json),所以如果可能的话,我认为我们必须使用预取来从服务器获取 json。我不想使用 getjson 将其捕获到变量中,还是有更简单的方法将其捕获到变量中?
更新 2:
what is wrong with my json coming from haystack - 创建了一个与此相关的新问题
【问题讨论】:
标签: javascript jquery json typeahead.js