---恢复内容开始---
<select id = "a1" onchange = "addOption()"></select>
<select id = "a2"></select>
<script type = "text/javascript">
var city = new Array;
city[‘江苏‘] = [‘南京‘,‘上海‘, ‘连云港‘, ‘苏州‘, ‘镇江‘,‘泰州‘,‘南通‘,‘淮安‘];
city[‘山东‘] = [‘青岛‘,‘烟台‘, ‘济南‘,‘曲阜‘,‘德州‘,‘滨州‘,‘临沂‘,‘菏泽‘];
city[‘湖南‘] = [‘长沙‘,‘株洲‘,‘湘潭‘,‘岳阳‘,‘娄底‘,‘常德‘,‘怀化‘,‘张家界‘];
function allCity() {
var select1 = document.getElementById("a1");
for(var i in city) { //这里注意遍历数组的写法
select1.add(new Option(i, i), null);
}
addOption(); // 初始化选项
}
function addOption() {
var select2 = document.getElementById("a2");
var select1 = document.getElementById("a1").value;
select2.length = 0; //每次都先清空一下市级菜单
if(select1 != ‘请选择省份‘) {
for(var i in city[select1 ]) {
select2.add(new Option(city[select1][i], city[select1][i]), null);
}
}else if (sheng == ‘请选择省份‘){
select2.length = 0;
select2.add(new Option("请选择城市", "请选择城市"), null);
}
}
window.onload = allCity();
</script>
---恢复内容结束---
原文地址:https://www.cnblogs.com/lfvkit/p/9009874.html