javascript省市聯動


<html>
<head>
<meta charset="utf-8">
<title>script</title>
<script tp="text/javascript">
var arr=new Array();
arr[arr.length]=[1,'北京市',null];
arr[arr.length]=[2,'四川省',null];
arr[arr.length]=[3,'福建省',null];
arr[arr.length]=[4,'浙江省',null];
arr[arr.length]=[16,'杭州市',4]
arr[arr.length]=[15,'北京市',1];
arr[arr.length]=[5,'成都市',2];
arr[arr.length]=[6,'福州市',3];
arr[arr.length]=[7,'廈門市',3];
arr[arr.length]=[8,'泉州市',3];
arr[arr.length]=[9,'漳州市',3];
arr[arr.length]=[10,'三明市',3];
arr[arr.length]=[11,'龍巖市',3];
arr[arr.length]=[12,'寧德市',3];
arr[arr.length]=[13,'莆田市',3];
arr[arr.length]=[14,'南平市',3];
function sb(tp){
if(tp=='shen'){
var shen=document.getElementById("shen");//獲得省的下拉列表
shen.innerHTML='';//內容爲空
var one='<option value=""></option>'//填充省的字符
for(var i=0; i<arr.length;i++){//遍歷數組
var item=arr[i];//當前項
if(item[2]==null)//
one+='<option value='+item[0]+'>'+item[1]+'</option>';

}
shen.innerHTML=one;//填充新內容
}else if(tp=='shi'){//獲得省份的Id
var three=document.getElementById("shen").value;
if(three=='')
return false;
var shi=document.getElementById("shi");//獲取下拉列表
shi.innerHTML="";
var two='<option value=""></option>';
for(var i=0;i<arr.length;i++){
var item=arr[i];
if(item[2]==three)
two+='<option value='+item[0]+'>'+item[1]+'</option>'
}
shi.innerHTML=two;
}
}
</script>
</head>
<body style="text-align:center的;" onLoad="sb('shen')">
<form>
姓 名:<input type="text" name="name"><br/>
密 碼:<input type="password" name="name"><br/>
省 份:<select id="shen" onChange="sb('shi')"></select><br/><br/>
城 市:<select id="shi"></select>
</form>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章