<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>三级菜单</title>
<script>
var arrSheng = new Array("湖南省", "湖北省", "广东省");
var arrShi = new Array(
new Array("长沙市", "株洲市", "湘潭市"),
new Array("武汉市", "荆州市", "宜昌市"),
new Array("广州市", "深圳市", "韶关市")
);
var arrQu = new Array(
new Array(
new Array("岳麓区", "芙蓉区", "天心区", "开福区"),
new Array("芦淞区", "荷塘区", "石峰区", "天元区", "云龙区"),
new Array("岳塘区", "雨湖区", "九华区")
),
new Array(
new Array("武昌区", "汉阳区", "青山区"),
new Array("荆州区", "沙市区"),
new Array("夷陵区", "西陵区", "伍家岗区")
),
new Array(
new Array("天河区", "越秀区", "白云区", "花都区"),
new Array("罗湖区", "福田区", "南山区", "盐田区", "宝安区", "龙岗区"),
new Array("浈江区", "武江区", "曲江区")
)
);
//添加省
function addSheng() {
sheng.length = 1;
for(i = 0; i < arrSheng.length; i++) {
// sheng.innerHTML += "<option>" + arrSheng[i] + "</option>";
// var opt = new Option(arrSheng[i]);
// sheng.add(opt);
sheng.add(new Option(arrSheng[i]));
}
}
//添加市
function addShi() {
//已选择的省对应的市的数组
var nowShi = arrShi[sheng.selectedIndex - 1];
shi.length = 1;
for(i = 0; i < nowShi.length; i++) {
shi.add(new Option(nowShi[i]));
}
}
//添加区
function addQu() {
//已选择的省(三维数组的一维索引)
var lv1 = sheng.selectedIndex - 1;
//已选择的市(三维数组的二维索引)
var lv2 = shi.selectedIndex - 1;
//已选择的省和市对应的区的数组
var nowQu = arrQu[lv1][lv2];
qu.length = 1;
for(i = 0; i < nowQu.length; i++) {
qu.add(new Option(nowQu[i]));
}
}
//添加省
function show() {
var choose1 = sheng.selectedIndex - 1;
var choose2 = shi.selectedIndex - 1;
var choose3 = qu.selectedIndex - 1;
showResult.innerHTML = "您选择的是" + arrSheng[choose1] + arrShi[choose1][choose2] + arrQu[choose1][choose2][choose3];
}
</script>
</head>
<body οnlοad="addSheng();">
<select id="sheng" οnchange="addShi();">
<option value="-1">--省--</option>
</select>
<select id="shi" οnchange="addQu();">
<option value="-1">--市--</option>
</select>
<select id="qu" οnchange="show();">
<option value="-1">--区--</option>
</select>
<div id="showResult"></div>
</body>
</html>
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>三级菜单</title>
<script>
var arrSheng = new Array("湖南省", "湖北省", "广东省");
var arrShi = new Array(
new Array("长沙市", "株洲市", "湘潭市"),
new Array("武汉市", "荆州市", "宜昌市"),
new Array("广州市", "深圳市", "韶关市")
);
var arrQu = new Array(
new Array(
new Array("岳麓区", "芙蓉区", "天心区", "开福区"),
new Array("芦淞区", "荷塘区", "石峰区", "天元区", "云龙区"),
new Array("岳塘区", "雨湖区", "九华区")
),
new Array(
new Array("武昌区", "汉阳区", "青山区"),
new Array("荆州区", "沙市区"),
new Array("夷陵区", "西陵区", "伍家岗区")
),
new Array(
new Array("天河区", "越秀区", "白云区", "花都区"),
new Array("罗湖区", "福田区", "南山区", "盐田区", "宝安区", "龙岗区"),
new Array("浈江区", "武江区", "曲江区")
)
);
//添加省
function addSheng() {
sheng.length = 1;
for(i = 0; i < arrSheng.length; i++) {
// sheng.innerHTML += "<option>" + arrSheng[i] + "</option>";
// var opt = new Option(arrSheng[i]);
// sheng.add(opt);
sheng.add(new Option(arrSheng[i]));
}
}
//添加市
function addShi() {
//已选择的省对应的市的数组
var nowShi = arrShi[sheng.selectedIndex - 1];
shi.length = 1;
for(i = 0; i < nowShi.length; i++) {
shi.add(new Option(nowShi[i]));
}
}
//添加区
function addQu() {
//已选择的省(三维数组的一维索引)
var lv1 = sheng.selectedIndex - 1;
//已选择的市(三维数组的二维索引)
var lv2 = shi.selectedIndex - 1;
//已选择的省和市对应的区的数组
var nowQu = arrQu[lv1][lv2];
qu.length = 1;
for(i = 0; i < nowQu.length; i++) {
qu.add(new Option(nowQu[i]));
}
}
//添加省
function show() {
var choose1 = sheng.selectedIndex - 1;
var choose2 = shi.selectedIndex - 1;
var choose3 = qu.selectedIndex - 1;
showResult.innerHTML = "您选择的是" + arrSheng[choose1] + arrShi[choose1][choose2] + arrQu[choose1][choose2][choose3];
}
</script>
</head>
<body οnlοad="addSheng();">
<select id="sheng" οnchange="addShi();">
<option value="-1">--省--</option>
</select>
<select id="shi" οnchange="addQu();">
<option value="-1">--市--</option>
</select>
<select id="qu" οnchange="show();">
<option value="-1">--区--</option>
</select>
<div id="showResult"></div>
</body>
</html>