DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> javascript實現簡單的省市區三級聯動
javascript實現簡單的省市區三級聯動
編輯:關於JavaScript     

當我們注冊一個網站,會看到省市區三級聯動,下面簡單介紹一下

<!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>

以上所述就是本文的全部內容了,希望大家能夠喜歡。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved