使用jQuery根据国家和州填充州和城市下拉列表 [英] Populate state and city dropdowns based on country and state using jQuery

查看:68
本文介绍了使用jQuery根据国家和州填充州和城市下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JSON完成下拉列表。我想要3个下拉菜单。首先填写国家/地区下拉菜单(例如:美国,英国等)。现在,当用户选择USA时,需要使用jQuery .change()填充下拉列表。再次当用户选择状态时,他们需要向城市显示下拉列表。

I am trying to accomplish dropdowns using JSON. I want 3 dropdowns. First populate the country dropdown (eg: usa, uk etc.,). Now, when the user select USA then states dropdown needs to be populated by using jQuery .change(). Again when user select the state they need to be presented with cities dropdowns.

我如何实现这一目标?由于我的JSON文件稍大,我在这里添加了它并尝试填充国家/地区下拉列表但无法生成州和城市下拉...

How can I achieve this? As my JSON file is slightly big I have added it here and tried to populate countries dropdown but unable to generate states and cities drop down...

http://jsfiddle.net/vCFv6/

$.each(myJson.country, function (index, value) {
$("#country").append('<option value="'+value.id+'">'+value.name+'</option>');});

上述代码可以帮助我填充国家,但不包括州和城市等其他国家/地区。非常感谢任何帮助。

The above code helps me populate just the countries but not others like states and cities. Any help is much appreciated.

提前致谢。

推荐答案

正如其他人所说,你必须处理国家和地区的 onchange 事件。州选择输入和运用你的逻辑。为了在选择国家时动态获取状态,我在这里摆弄,你可以自己编写其余的代码 - Fiddle

Exactly like others said, you have to handle the onchange event of country & state select inputs & apply your logic. I have fiddled here for getting states dynamically on selecting a country, you might be able to code the rest yourself - Fiddle

您可能还会看到基于其他下拉值的
填充下拉列表

这篇关于使用jQuery根据国家和州填充州和城市下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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