城市联动选择菜单,网上常常见到,很少介绍了,本款城市选择菜单原型基于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>