城市聯動選擇菜單,網上常常見到,很少介紹了,本款城市選擇菜單原型基於Select,主要使用JavaScript來實現,運用了數組和循環等基礎技巧製做完成的。本效果只是爲了演示如何實現,裏面的數據不全,須要的本身能夠添加哦。html
<html>數組
<head>app
<title>Js城市二級聯動選擇插件</title>ide
<script>this
var citys=new Array(插件
new Array("南京","淮安","揚州","常州",'其它'),htm
new Array("北京"),ip
new Array("天津"),ci
new Array("上海"),get
new Array("其它")
);
function scity(pname,cname){
var province=['江蘇省','北京','天津','上海','其它'];
document.write('<select id="pro" onchange="selectc(this)" name="'+pname+'">');
document.write('<option value="">--選擇省份--</option>')
for(var i=0;i<province.length;i++){
document.write('<option value="'+province[i]+'">'+province[i]+'</option>');
}
document.write('</select>');
document.write('<select id="city" name="'+cname+'">');
document.write('<option value="">--選擇城市--</option>');
document.write('</select>');
selectc(document.getElementById("pro"));
}
function selectc(pobj){
var index=pobj.selectedIndex-1;
var cobj=document.getElementById("city");
cobj.innerHTML='';
if(index>=0){
for(var i=0;i<citys[index].length;i++){
var option=document.createElement("option");
var text=citys[index][i];
option.value=text;
option.innerHTML=text;
cobj.appendChild(option);
}
}else{
var option=document.createElement("option");
option.value="";
option.innerHTML="--選擇城市--";
cobj.appendChild(option);
}
}
</script>
</head>
<body>
<script>
scity('p','c');
</script>
</body>
</html>