基於JQuery地區三級聯動列表

來源:互聯網
上載者:User

手頭做的項目中需要用到地區庫,在網上找了幾個地區庫,感覺不是很好,於是著手自己生 成一個地區庫.地區來自中國統計局網站(資料夠官方)(地址是: http://www.stats.gov.cn/tjbz/xzqhdm/t20080215_402462675.htm),通過文本處理,產生到 資料庫(需要該地區資料庫的朋友可留下郵箱地址,我發給你),做可處理操作.

這次用到的聯動是產生了一個地區的js檔案,原來是採用AJAX實現聯動的,後來感覺產生JS 檔案並不大,所以採用js方式.

下面是js處理代碼(基於JQuery):

<script type="text/javascript">
    $(document).ready(function(){
      getProvinces();
    });
    
    function getProvinces(){
      var pro = "";
      for(var i = 0 ; i < provinces.length; i++){
        pro += "<option>" + provinces[i] + "</option>";
      }
      $('#province').empty().append(pro);
      getCities();
    }
    function getCities(){
      var proIndex = $('#province').attr('selectedIndex');
      showCities(proIndex);
      getCounties();
    }
    function showCities(proIndex){
      var cit = "";
      if(cities[proIndex] == null){
        $('#city').empty();
        return;
      }
      for(var i = 0 ;i < cities[proIndex].length ; i++){
        cit += "<option>" + cities[proIndex][i] + "</option>";
      }
      $('#city').empty().append(cit);
    }
    function getCounties(){
      var proIndex = $('#province').attr('selectedIndex');
      var citIndex = $('#city').attr('selectedIndex');
      showCounties(proIndex,citIndex);
    }
    function showCounties(proIndex,citIndex){
      var cou = "";
      if(counties[proIndex][citIndex] == null){
        $('#county').empty();
        return;
      }
      for(var i = 0 ;i < counties[proIndex][citIndex].length;i++){
        cou += "<option>" + counties[proIndex][citIndex] [i] + "</option>";
      }
      $('#county').empty().append(cou);
    }
    </script><select id="province" onchange="getCities()"></select>
          <select id="city" onchange="getCounties() " style="width:120px;"></select>
          <select id="county" style="width:120px;"></select>

這樣實現了一個比較好的地區聯動.

本文配套源碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.