JS省市二级级联,选择省无法关联到市,求指导
<html><metacharset="UTF-8"><head><scriptlanguage="javascript"type="text/javascript">v...
<html>
<meta charset="UTF-8">
<head>
<script language = "javascript" type = "text/javascript">
var prov = new Array();
prov[0] = "江苏省";
prov[1] = "浙江省";
prov[2] = "安徽省";
var citys = new Array();
citys[0] = new Array();
citys[1] = new Array();
citys[2] = new Array();
citys[0][0] = "南京市";
citys[0][1] = "苏州市";
citys[0][2] = "无锡市";
citys[1][0] = "杭州市";
citys[1][1] = "宁波市";
citys[1][2] = "嘉兴市";
citys[2][0] = "合肥市";
citys[2][1] = "芜湖市";
function changeCity(){
var p = document.getElementById("province");
var city = document.getElementById("city");
var pvalue = p.options[p.selectedIndex].value;//获取选择省份的值
city.options.length = 0;//清空城市数组长度
//从citys二维数组中遍历构建option选项
for(var i=0;i<citys[pvalue].length;i++){
var text = citys[pvalue][i];
var value = i;
city.options[city.options.length] = new option(text,value);
}
}
</script>
</head>
<body >
省份:
<select name="province" id="province" onChange="changeCity()">
<option value="a" selected>请选择省份</option>
<script language = "javascript" type = "text/javascript">
for(var i=0;i< prov.length;i++){
document.write("<option value='"+i+"'>" + prov[i] + "</option>");
}
</script>
</select>
城市:
<select name="city" id="city">
<option value="h">请选择城市</option>
</select>
</body>
</html> 展开
<meta charset="UTF-8">
<head>
<script language = "javascript" type = "text/javascript">
var prov = new Array();
prov[0] = "江苏省";
prov[1] = "浙江省";
prov[2] = "安徽省";
var citys = new Array();
citys[0] = new Array();
citys[1] = new Array();
citys[2] = new Array();
citys[0][0] = "南京市";
citys[0][1] = "苏州市";
citys[0][2] = "无锡市";
citys[1][0] = "杭州市";
citys[1][1] = "宁波市";
citys[1][2] = "嘉兴市";
citys[2][0] = "合肥市";
citys[2][1] = "芜湖市";
function changeCity(){
var p = document.getElementById("province");
var city = document.getElementById("city");
var pvalue = p.options[p.selectedIndex].value;//获取选择省份的值
city.options.length = 0;//清空城市数组长度
//从citys二维数组中遍历构建option选项
for(var i=0;i<citys[pvalue].length;i++){
var text = citys[pvalue][i];
var value = i;
city.options[city.options.length] = new option(text,value);
}
}
</script>
</head>
<body >
省份:
<select name="province" id="province" onChange="changeCity()">
<option value="a" selected>请选择省份</option>
<script language = "javascript" type = "text/javascript">
for(var i=0;i< prov.length;i++){
document.write("<option value='"+i+"'>" + prov[i] + "</option>");
}
</script>
</select>
城市:
<select name="city" id="city">
<option value="h">请选择城市</option>
</select>
</body>
</html> 展开
1个回答
展开全部
for (var i = 0; i < citys[pvalue].length; i++) {
var text = citys[pvalue][i];
var value = i;
var option = document.createElement("option");
option.innerText = text;
city.appendChild(option);
//city.options[city.options.length] = new option(text, value);
}
for循环里改一下
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询