Javascript JQuery:如何自动完成“城市、州”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2586747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery: How to AutoComplete "City, State"?
提问by NickT
Question: How can you use the JQuery Auto-Completion pluginto suggest a location ("City, State") for an input field?
问题:如何使用JQuery 自动完成插件"City, State"为输入字段建议位置 ( )?
Meaning, someone wants to type in "Chicago, IL"... so they begin typing "Chi"and it auto-suggestions "Chicago, IL".
意思是,有人想输入“芝加哥,伊利诺伊州”......所以他们开始输入"Chi"并自动建议"Chicago, IL“。
My biggest hurdles is finding a service that I can query to find out all US city+state names.
我最大的障碍是找到一项服务,我可以查询该服务以找出所有美国城市+州的名称。
I essentially want to do what the StackOverflow "Tags" input form works but form "City, State" auto completion.
我基本上想做 StackOverflow“标签”输入表单的工作,但表单“城市,州”自动完成。
回答by Jacob Mattison
There's a group called GeoNames that offers both downloadable databases and free webservices to obtain this type of data. For example, the search
有一个名为 GeoNames 的组织,它提供可下载的数据库和免费的网络服务来获取此类数据。例如,搜索
http://ws.geonames.org/searchJSON?name_startsWith=Chic&country=US
will return a bunch of JSON that includes Chicago, IL (JSON properties include, among others, "name":"Chicago" and "adminCode1":"IL").
将返回一堆 JSON,其中包括芝加哥,IL(JSON 属性包括“名称”:“芝加哥”和“adminCode1”:“IL”)。
As it happens, the jQuery UI folks used this datasource for a demo of autocomplete. (Thanks NickT for the link).
碰巧的是,jQuery UI 人员使用这个数据源来演示自动完成。(感谢 NickT 提供链接)。
回答by Jeremy
You either need to make an ajax call to the server whenever a key is pressed to update the "auto complete" drop down, or include an array of all the possible "city-state" strings and filter them when a key is pressed.
您要么需要在按下某个键时对服务器进行 ajax 调用以更新“自动完成”下拉列表,要么包含所有可能的“城市状态”字符串的数组,并在按下某个键时对其进行过滤。
回答by Magnus Hiie
@Teleportoffers an API to search cities by name, e.g.: https://api.teleport.org/api/cities/?search=San,%20United%20States%20of%20Americareturns San Antonio, San Diego, San Jose and San Francisco. You can also pass state, e.g. https://api.teleport.org/api/cities/?search=San,%20Tex,%20United%20States%20of%20Americaonly returns San Antonio. And you can also search by aiport codes (e.g. SFO) and other "aliases".
@Teleport提供按名称搜索城市的API,例如:https: //api.teleport.org/api/cities/? search =San,% 20United%20States%20of%20America返回圣安东尼奥、圣地亚哥、圣何塞和旧金山。你也可以传递状态,例如https://api.teleport.org/api/cities/?search=San,%20Tex,%20United%20States%20of%20America只返回圣安东尼奥。您还可以通过机场代码(例如 SFO)和其他“别名”进行搜索。
There's also an autocomplete widget for this API available at https://github.com/teleport/autocomplete
在https://github.com/teleport/autocomplete上还有一个用于此 API 的自动完成小部件
Disclosure: I work for Teleport.
披露:我为 Teleport 工作。
EDIT: Added "of America" suffix in the "United States of America" search term, as some other countries (Brazil, Venezuela) have name forms that match "United States".
编辑:在“美利坚合众国”搜索词中添加了“of America”后缀,因为其他一些国家(巴西、委内瑞拉)的名称形式与“美国”匹配。

