javascript 从动态下拉列表中选择国家/州

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13399501/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:38:55  来源:igfitidea点击:

Selected Country / State From Dynamic Dropdown List

phpjavascriptjquery

提问by Saint Robson

I'm using this Country / State dynamic selection on my code :

我在我的代码中使用了这个国家/州动态选择:

http://bdhacker.wordpress.com/2009/11/21/adding-dropdown-country-state-list-dynamically-into-your-html-form-by-javascript/

http://bdhacker.wordpress.com/2009/11/21/adding-dropdown-country-state-list-dynamically-into-your-html-form-b​​y-javascript/

and here's the demo page : http://bdhacker.sourceforge.net/javascript/countries/

这是演示页面:http: //bdhacker.sourceforge.net/javascript/countries/

but, unfortunately when I create setting / member profile page which I need to display selected country and state so user can see what they already chosen, I can't just put user's selected as 'value' on it.

但是,不幸的是,当我创建设置/成员个人资料页面时,我需要显示选定的国家和州,以便用户可以看到他们已经选择的内容,我不能只是将用户选择为“价值”。

here's what I see on page source even I have select a country and a state :

这是我在页面源上看到的内容,即使我选择了一个国家和一个州:

Select Country: <select onchange="print_state('state',this.selectedIndex);" id="country" name ="country"></select>

City/District/State: <select name ="state" id ="state"></select>

how to put value on that ? because I did like this :

如何赋予它价值?因为我确实喜欢这样:

Select Country:   
<select onchange="print_state('state',this.selectedIndex);" id="country" name ="country" value="Indonesia"></select>

City/District/State: <select name ="state" id ="state"></select>

and still I don't see there's a country selected. thanks!

我仍然没有看到选择了一个国家。谢谢!

回答by br3t

try this solution http://jsfiddle.net/yDAmR/

试试这个解决方案http://jsfiddle.net/yDAmR/

print_country("country");
$('#country').val('Indonesia');
print_state('state',$('#country')[0].selectedIndex);

回答by Thomas Gemal

The reason you see the USA states for other countries is that the JavaScript is missing a reset of the state drop-down list. Just add "option_str.length=0; // Clear list" in the JS file before you split list array.

您看到其他国家的美国州的原因是 JavaScript 缺少州下拉列表的重置。在拆分列表数组之前,只需在 JS 文件中添加“option_str.length=0; // 清除列表”即可。