我对 jquery 比较陌生,正在尝试弄清楚如何让这个自动建议插件与地理名称一起使用(返回位置信息)。在http://jqueryui.com/demos/autocomplete/#remote-jsonp有一个类似的使用 jquery 自动完成的例子,但我无法让它与 autosuggest 一起使用。以下是我正在尝试的,但我没有得到任何结果和此浏览器控制台错误:未捕获的类型错误:无法读取未定义的属性“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 复制到一个名为 data 的 var 中,然后将它用于这样的源,它可以工作:
$("#location2").autoSuggest("data.geonames", {selectedItemProp: "adminName1", searchObjProps: "adminName1", selectedValuesProp : "name"});
我还想知道在选择结果时是否可以组合多个值,例如 adminName1 + name?
谢谢,麻烦您了!