JS中的开源国家/地区选择器 [英] Open Source Country Region Selector in JS

查看:597
本文介绍了JS中的开源国家/地区选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的项目开发库/框架.我想做一个国家选择器,

I am working on a library/framework for my project. I would like to do a Country selector,

用户选择美国",然后在状态"框中填充所有州

User picks USA it then populates a States box with all states

用户选择加拿大,然后在另一个框中填充加拿大的所有地区

User picks Canada it then populates another box with all regions for Canada

用户选择墨西哥,然后在另一个框中填充墨西哥的所有区域

User picks Mexico it then populates another box with all regions for Mexico

用户选择英国,然后在另一个框中填充英国的所有地区

User picks UK it then populates another box with all regions for UK

等...您就知道了,因为它将成为框架/库的一部分,我想使其变得完整且易于使用.所以我想添加许多不同的国家和地区组合

etc... you get the idea, since it is going to be part of a framework/library I would like to make it ass complete and easy to use. SO I would like to add many different country and region combinations

如果在将值保存到数据库或文件中或作为用户设置在任何地方的项目中使用,则在页面加载且有值可用时,它应该预先填充正确的选择框并具有已经选择了正确的国家和地区.

Also if this is used in a project where the values are saved into a database or file or anywhere as a user setting, then when the page loads and a value is available, it should pre-populate the correct selection boxes and have the correct Country and region selected already.

现在,我知道需要Javascript,并且我使用的是jQuery,因此最好使用jQuery和PHP.

Now I know Javascript is required, and I am using jQuery so It might be best to use jQuery, and also PHP.

1)
在一个个人项目中,我可能会将所有国家和地区组合存储在一个MySQL数据库中,但是由于我希望这只是解决方案中的一个问题,因此最好将包含有Country选项的文件存储在PHP数组中然后将每个区域集放在一个单独的文件中作为数组.

1)
On a personal project, I would probably store all the country and region combinations in a MySQL database however since I want to have this be more of a drop in solution, would it be best to store a file containing ever Country option in a PHP array and then have each set of regions in a separate file as an array.

因此,美国将有一个单独的文件,其中包含50个州的PHP数组,加拿大将有一个文件,其中包含所有不同的地区/地区等.还是有更好的方法? 或者我应该继续将所有这些存储在MySQL或SQLite中,并提供带有代码的适当表

So the United States would have a separate file that would contain a PHP array of the 50 states, Canada would have a file with all it's different staes/regions etc.. Or is there a better way? OR should I go ahead and just store all this in MySQL or SQLite and provide the proper tables with the code

2) 根据教程和内容,我想我可以弄清楚如何根据从另一个选择框选择的国家/地区来填充地区/州"选择框,但是,如果用户以前选择了美国和纽约,我该怎么办确保第一个框被选中为美国,第二个框已为美国地区/州加载了文件/内容,然后在该列表中选择了正确的州

2) Based off tutorials and stuff I think I can figure out how to make a Region/State selection box populate based off the Country that is selected from another selection box however, if a user have previously selected United States and New York, how would I go about making sure the first box is selected United States and the second box has loaded the file/contents for United States regions/states and then selected the proper states in that list

3) 您是否知道这样的事物已经可以帮助我更多,或者您是否愿意做出任何贡献,即使使我朝着正确的方向前进也将有所帮助,并且将不胜感激

3) Do you know of anything that exist like this already to help me more or if you would like to contribute anything even getting me in the right direction will help and be greatly appreciated

推荐答案

Google for javascript country state dropdown,您会发现很多解决方案.第一个位于 http://javascriptsource.com/forms/country-state-drop -down.html 似乎已经很合理了.它将所有数据存储在JavaScript代码中-毕竟,组合数量不多.对于缺少州/地区信息的国家/地区,用户可以输入任意区域.

Google for javascript country state dropdown and you'll find lots of solutions. The first one at http://javascriptsource.com/forms/country-state-drop-down.html already seems pretty reasonable. It stores all the data in the JavaScript code—after all, there are not that many combinations. For countries where it lacks state/region information, the user can enter an arbitrary region.

这篇关于JS中的开源国家/地区选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆