<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function showCity(){
//維護一個二維數組存儲省份對應的城市
var citys = [[],["廣州","佛山","湛江","中山"],["長沙","衡陽","岳陽","郴州"],["南寧","桂林","貴港","柳州"]];
//獲取省份對應的節點
var provinceNode = document.getElementById("province");
// 獲取省份選中的選項
var selectIndex = provinceNode.selectedIndex;
// 獲取對應的城市
var cityDatas = citys[selectIndex];
// 找到city節點
var cityNode = document.getElementById("city");
// 先要清空city框所有option
/*var children = cityNode.childNodes;
for(var i=0; i<children.length; ){
cityNode.removeChild(children[i]);
}
*/
// 設置option的個數
cityNode.options.length=1;
// 遍歷對應的所有城市然後創建對應的option添加到city上
for(var index=0; index<cityDatas.length; index++){
var option = document.createElement("option");
option.innerHTML = cityDatas[index];
cityNode.appendChild(option);
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>
<body>
省份<select id="province" οnchange="showCity()">
<option>省份</option>
<option>廣東</option>
<option>湖南</option>
<option>廣西</option>
</select>
城市<select id="city">
<option>城市</option>
</select>
</body>
</html>
城市的聯動框
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.