本文給大家反映的是javascript實現的簡單的省市區三級聯動特效,不需要訪問後台服務器端,不使用Ajax,無刷新,純JS實現的省市區三級聯動。當省市區數據變動是只需調正js即可。
當我們注冊一個網站,會看到省市區三級聯動,下面簡單介紹一下
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>Document</title> </head> <body> <select id="province" onChange="ck()" > <option>--請選擇--</option> </select> <select id="city"> <option>--請選擇--</option> </select> <select id="san"> <option>--請選擇--</option> </select> </body> <script type="text/javascript"> //======================================第一種方法:獲取省市二級聯菜單============================= //獲取省的id var prame= document.getElementById("province"); var city= document.getElementById("city"); var san= document.getElementById("san"); //創建省市數組 var cityList=new Array(); //創建另一個數組 var zushu=new Array; cityList['江西省'] = ['南昌市','撫州市','上饒市','鷹潭市','豐城市','吉安市']; cityList['河南省'] = ['鄭州市','洛陽市']; cityList['湖北省'] = ['武漢市','宜昌市']; cityList['福建省'] = ['福州市','三明市','佛山市']; zushu['南昌市']=['南昌縣','青雲譜區','蓮塘鎮']; zushu['撫州市']=['臨川區','雲山鎮','唱凱鎮']; for(var i in cityList){ prame.add(new Option(i,i),null); } prame.onchange=function(){ var prame= document.getElementById("province").value; var city= document.getElementById("city"); city.options.length=0; for(var k in cityList[prame]){ city.add(new Option(cityList[prame][k],cityList[prame][k]),null); } } city.onchange=function(){ var city= document.getElementById("city").value; var san= document.getElementById("san"); san.options.length=0; for(var k in zushu[city]){ san.add(new Option(zushu[city][k]),(zushu[city][k]),null); } } </script> <!--------------------------------第二種方法:三級聯動菜單---------------------- <script type="text/javascript"> //獲取省的id var province=document.getElementById("province"); province.add(new Option("江西省","江西省"),null); province.add(new Option("福建省","福建省"),null); province.add(new Option("廣東省","廣東省"),null); function ck(){ var city=document.getElementById("city"); var num=province.selectedIndex; city.options.length=0; switch(num){ case 1: city.add(new Option("南昌市","南昌市"),null); city.add(new Option("撫州市","撫州市"),null); city.add(new Option("上饒市","上饒市"),null); city.add(new Option("吉安市","吉安市"),null); break; case 2: city.add(new Option("廈門市","廈門市"),null); city.add(new Option("三明市","三明市"),null); city.add(new Option("莆田市","莆田市"),null); city.add(new Option("龍巖市","龍巖市"),null); break; case 3: city.add(new Option("廣州市","廣州市"),null); city.add(new Option("東莞市","東莞市"),null); city.add(new Option("保定市","保定市"),null); city.add(new Option("珠海市","珠海市"),null); break; } var qu=document.getElementById("qu"); var num1=province.selectedIndex; qu.options.length=0; switch(num1){ case 1: qu.add(new Option("臨川區","臨川區"),null); qu.add(new Option("青雲譜區","青雲譜區"),null); qu.add(new Option("南昌鎮","南昌鎮"),null); qu.add(new Option("雲山鎮","雲山鎮"),null); break; case 2: qu.add(new Option("保安縣","保安縣"),null); qu.add(new Option("福田區","福田區"),null); qu.add(new Option("將樂縣","將樂縣"),null); qu.add(new Option("廈門區","廈門區"),null); break; case 3: qu.add(new Option("廣上縣","光上縣"),null); qu.add(new Option("西莞區","西莞區"),null); qu.add(new Option("三海縣","三海縣"),null); qu.add(new Option("廣島區","廣島區"),null); break; } } </script>--> </html>以上所述就是本文的全部內容了,希望大家能夠喜歡。