Js做的城市三级联动,原创
说实话这方面的文章在网上很多,不过我下载了几个看了一下代码,发现都是出自一个人的手笔,而且那个联动很不好用,所以呢我就按照自己的想法再次写了一下这东西,分享给大家!欢迎转载但请注明出处Email:See7di@Gmail.com,Seven的部落格http://hi.baidu.com/see7di/home
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>国家、省、市三级联动</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<SCRIPT LANGUAGE="JavaScript">
<!--
function cn(){
this.Items = {};
}
cn.prototype.add = function(id,iArray){
this.Items[id] = iArray;
}
cn.prototype.Exi = function(id){
if(typeof(this.Items[id]) == "undefined"){return false;}
return true;
}
function chg(v){
if(v==3){return;}
var str="0";
for(i=0;i<v;i++){str+=("_"+(document.getElementById(s[i]).selectedIndex))};
var ss=document.getElementById(s[v]);
with(ss){
length = 0; //清理掉option
if(a_t.Exi(str)){
ar=a_t.Items[str];
for(i=0;i<ar.length;i++){
options[length]=new Option(ar[i],ar[i]);
if(ar[i]==o[v]){options[i].selected=true;}//如果列表内包含初始项目则默认选中
}
}
if(++v<s.length){chg(v);}
}
}
var s=["tid1","tid2","tid3"];
var o=["台湾","桃园","八德市"];//迎欢转载但请注明出处Email:See7di@Gmail.com,Seven的部落格http://hi.baidu.com/see7di/home
function setup(){
for(i=1;i<=3;i++){
document.getElementById(s[(i-1)]).onchange=new Function("chg("+(i)+")");
}
chg(0);
}
var a_t = new cn();
a_t.add("0",["中国","韩国","台湾"]);
a_t.add("0_0",["安徽","北京","福建","甘肃","广东","广西","贵州","海南","河北","河南","黑龙江","湖北","湖南","吉林","江苏","江西","辽宁","内蒙古","宁夏","青海","山东","山西"]);
a_t.add("0_0_0",["安庆","蚌埠","巢湖","池州","滁州","阜阳","合肥","淮北","淮南","黄山","六安","马鞍山","宿州","铜陵","芜湖","宣城","亳州"]);
a_t.add("0_0_1",["北京"]);
a_t.add("0_0_2",["福州","龙岩","南平","宁德","莆田","泉州","三明","厦门","漳州"]);
a_t.add("0_0_3",["白银","定西","甘南藏族自治州","嘉峪关","金昌","酒泉","兰州","临夏回族自治州","陇南","平凉","庆阳","天水","武威","张掖"]);
a_t.add("0_0_4",["潮州","东莞","佛山","广州","河源","深圳","阳江","云浮","湛江","肇庆","中山","珠海"]);
a_t.add("0_0_5",["百色","北海","崇左","防城港","桂林","贵港","河池","贺州","来宾","柳州","南宁","钦州","梧州","玉林"]);
a_t.add("0_0_6",["安顺","毕节","贵阳","六盘水","黔东南苗族侗族自治州","黔南布依族苗族自治州","黔西南布依族苗族自治州","铜仁","遵义"]);
a_t.add("0_0_7",["澄迈县","定安县","东方","海口",,"琼中黎族苗族自治县","三亚","屯昌县","万宁","文昌","五指山","儋州"]);
a_t.add("0_0_8",["保定","沧州","承德","邯郸","衡水","廊坊","秦皇岛","石家庄","唐山","邢台","张家口"]);
a_t.add("0_0_9",["安阳","鹤壁","济源","焦作","开封","洛阳","南阳","平顶山","三门峡","商丘","新乡","信阳","许昌","郑州","周口","驻马店","漯河","濮阳"]);
a_t.add("0_0_10",["大庆","大兴安岭","哈尔滨","鹤岗","黑河","鸡西","佳木斯","牡丹江","七台河","齐齐哈尔","双鸭山","绥化","伊春"]);
a_t.add("0_0_11",["鄂州","恩施土家族苗族自治州","黄冈","黄石","荆门","荆州","潜江","神农架林区","十堰","随州","天门","武汉","仙桃","咸宁","襄樊","孝感","宜昌"]);
a_t.add("0_0_12",["常德","长沙","郴州","衡阳","怀化","娄底","邵阳","湘潭","湘西土家族苗族自治州","益阳","永州","岳阳","张家界","株洲"]);
a_t.add("0_0_13",["白城","白山","长春","吉林","辽源","四平","松原","通化","延边朝鲜族自治州"]);
a_t.add("0_0_14",["常州","淮安","连云港","南京","南通","苏州","宿迁","泰州","无锡","徐州","盐城","扬州","镇江"]);
a_t.add("0_0_15",["抚州","赣州","吉安","景德镇","九江","南昌","萍乡","上饶","新余","宜春","鹰潭"]);
a_t.add("0_0_16",["鞍山","本溪","朝阳","大连","丹东","抚顺","阜新","葫芦岛","锦州","辽阳","盘锦","沈阳","铁岭","营口"]);
a_t.add("0_0_17",["阿拉善盟","巴彦淖尔盟","包头","赤峰","鄂尔多斯","呼和浩特","呼伦贝尔","通辽","乌海","乌兰察布盟","锡林郭勒盟","兴安盟"]);
a_t.add("0_0_18",["固原","石嘴山","吴忠","银川"]);
a_t.add("0_0_19",["果洛藏族自治州","海北藏族自治州","海东","海南藏族自治州","海西蒙古族藏族自治州","黄南藏族自治州","西宁","玉树藏族自治州"]);
a_t.add("0_0_20",["滨州","德州","东营","菏泽","济南","济宁","莱芜","聊城","临沂","青岛","日照","泰安","威海","潍坊","烟台","枣庄","淄博"]);
a_t.add("0_1",["汉城特別市","釜山广域市","大邱广域市","济州道"]);
a_t.add("0_1_0",["汉城"]);
a_t.add("0_1_1",["釜山","机张郡"]);
a_t.add("0_1_2",["大邱","达城郡"]);
a_t.add("0_1_3",["仁川","江华郡","瓮津郡"]);
a_t.add("0_1_4",["光州"]);
a_t.add("0_1_5",["大田"]);
a_t.add("0_1_6",["蔚山","蔚州郡"]);
a_t.add("0_1_7",["水原市","城南市","安山市","高阳市","安养市","富川市"]);
a_t.add("0_1_8",["春川市","原州市","江陵市"]);
a_t.add("0_1_9",["清州市"]);
a_t.add("0_1_10",["天安市"]);
a_t.add("0_1_11",["全州市","群山市","益山市"]);
a_t.add("0_1_12",["木浦市","丽水市","顺天市"]);
a_t.add("0_1_13",["浦项市","龟尾市","庆州市"]);
a_t.add("0_1_14",["昌原市","马山市","晋州市"]);
a_t.add("0_1_15",["济州市","西归浦市","北济州郡","南济州郡"]);
a_t.add("0_2",["基隆","台北","桃园","新竹","苗栗","台中","彰化","南投","云林","嘉义","台南","高雄","台东","花莲"]);
a_t.add("0_2_0",["基隆"]);
a_t.add("0_2_1",["台北"]);
a_t.add("0_2_2",["中壢市","平鎮市","龍潭鄉","楊梅鎮","新屋鄉","觀音鄉","龜山鄉","八德市","大溪鎮","復興鄉","大園鄉","蘆竹鄉"]);
a_t.add("0_2_3",["名古屋市 "]);
a_t.add("0_2_4",["札幌市"]);
a_t.add("0_2_5",["神戸市"]);
a_t.add("0_2_6",["京都市"]);
a_t.add("0_2_7",["福冈市"]);
a_t.add("0_2_8",["川崎市"]);
a_t.add("0_2_9",["埼玉市"]);
a_t.add("0_2_10",["广岛市"]);
a_t.add("0_2_11",["仙台市"]);
a_t.add("0_2_12",["北九州市 "]);
a_t.add("0_2_13",["千叶市"]);
//-->
</SCRIPT>
</head>
<body onLoad="setup()">
<form method='post' action='@.php'>
<select id="tid1" name="tid1"></select>
<select id="tid2" name="tid2"></select>
<select id="tid3" name="tid3"></select>
<input type='submit' value='submit'>
</form>