标题
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>省市级联效果</title>
<script type="text/javascript">
function selectCity(){
var objProvince=document.getElementById("province");
var objCity=document.getElementById("city");
//objCity.length=0; //清空下拉列表选项
objCity.options.length=0;
switch(objProvince.value){
case "1":
objCity.add(new Option("南京","1"),null);
objCity.add(new Option("苏州","2"),null);
objCity.add(new Option("无锡","3"),null);
objCity.add(new Option("南通","4"),null);
break;
case "2":
objCity.add(new Option("济南","1"),null);
objCity.add(new Option("青岛","2"),null);
objCity.add(new Option("日照","3"),null);
objCity.add(new Option("烟台","4"),null);
break;
case "3":
objCity.add(new Option("广州","1"),null);
objCity.add(new Option("东莞","2"),null);
objCity.add(new Option("深圳","3"),null);
objCity.add(new Option("佛山","4"),null);
break;
}
}
</script>
</head>
<body>
<form action="" method="post">
<select id="province" onchange="selectCity();">
<option value="0">--省份--</option>
<option value="1">江苏省</option>
<option value="2">山东省</option>
<option value="3">广东省</option>
<option value="4">浙江省</option>
<option value="5">云南省</option>
</select>
<select id="city">
<option value="0">--城市--</option>
</select>
</form>
</body>
</html>
数组-省市级联
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>数组-省市级联</title>
<script type="text/javascript">
//定义省份城市数组
var provinces=new Array();
provinces["江苏省"]=["南京市","苏州市","徐州市","宿迁市"];
provinces["山东省"]=["济南市","日照市","烟台市","青岛市"];
provinces["浙江省"]=["杭州市","宁波市","义务市","温州市"];
provinces["广东省"]=["广州市","佛山市","东莞市","珠海市"];
//初始化省份信息
function init(){
var objProvince=document.getElementById("province");
for(var p in provinces){
objProvince.add(new Option(p,p),null);
}
}
//切换城市
function changeCity(){
var objProvince=document.getElementById("province");
var objCity=document.getElementById("city");
objCity.length=0;
//方式一:
/* for(var p in provinces){
if(p==objProvince.value){
for(var c in provinces[p]){
objCity.add(new Option(provinces[p][c],provinces[p][c]),null);
}
}
} */
//方式二:
for(var p in provinces[objProvince.value]){
objCity.add(new Option(provinces[objProvince.value][p],provinces[objCity.value]),null);
}
}
window.onload=init;
</script>
</head>
<body>
<form action="" method="post">
<select id="province" onchange="changeCity();">
<option value="0">--省份--</option>
</select>
<select id="city">
<option value="0">--城市--</option>
</select>
</form>
</body>
</html>
时间: 2024-12-25 20:35:56