我对jquery比较陌生,我想弄清楚如何让这个autosuggest插件使用geonames(返回位置信息)。有一个类似的例子,它在这里使用jquery自动完成http://jqueryui.com/demos/autocomplete/#remote-jsonp,但我不能让它与autosuggest一起工作。下面是我正在尝试的但是我得不到任何结果和浏览器控制台错误:未捕获TypeError:无法读取未定义的属性'geonames.adminName1'。
$("#location2").autoSuggest("http://ws.geonames.org/searchJSON", {selectedItemProp: "geonames.adminName1", searchObjProps: "geonames.adminName1", selectedValuesProp : "geonames.name"});
从geonames返回的json看起来像这样:
{"totalResultsCount":29,"geonames":[{"countryName":"United States","adminCode1":"UT","fclName":"city, village,...","countryCode":"US","lng":-111.6946475,"fcodeName":"populated place","toponymName":"Orem","fcl":"P","name":"Orem","fcode":"PPL","geonameId":5779334,"lat":40.2968979,"adminName1":"Utah","population":88328},{"countryName":"United States","adminCode1":"UT","fclName":"country, state, region,...","countryCode":"US","lng":-111.69912,"fcodeName":"administrative division","toponymName":"City of Orem","fcl":"A","name":"City of Orem","fcode":"ADMD","geonameId":7174024,"lat":40.2987069,"adminName1":"Utah","population":88328}]}
如果我手动将JSON复制到一个名为var的var中,然后像这样使用它来工作:
$("#location2").autoSuggest("data.geonames", {selectedItemProp: "adminName1", searchObjProps: "adminName1", selectedValuesProp : "name"});
我还想知道在选择结果时是否可以组合多个值,例如adminName1 + name?
拜托,谢谢!
答案 0 :(得分:0)
自动完成时的远程jsonp响应与$ .ajax()方法一起使用。 查看有关$ .ajax()的文档。
成功之后,ajax方法可以将数据传递给$ .map()然后你可以用数据做任何你想做的事情(打印出来或把它放在DOM上)