使用区划代码的省市县三级联动下拉框

2 篇文章 0 订阅
test.html 
Html代码    收藏代码
  1. <!doctype html>  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html;charset=utf-8">  
  5. <script type="text/javascript" src="../js/jquery-1.4.2.js"></script>  
  6. <script type="text/javascript" src="Area.js"></script>  
  7. <style>  
  8. body{font-size:14px; font-family:simsun;margin:0;}  
  9. </style>  
  10. </head>  
  11. <body>  
  12. <span id="areaContainer">  
  13.     <select></select><select></select><select></select>  
  14. </span>  
  15. <input id="getValueBtn" type="button" value="getValue">  
  16. </body>  
  17. </html>  
  18. <script type="text/javascript">  
  19. $(function(){  
  20.     Area.init('area2.json');  
  21.     var area  = new Area('#areaContainer');  
  22.     area.select('450802');  
  23.       
  24.     $('#getValueBtn').click(function(){  
  25.         var code = area.getAddress();  
  26.         var address = Area.getAddressByCode(code);  
  27.         alert(code + address);  
  28.     });  
  29. });  
  30. </script>  


Area.js 
Js代码    收藏代码
  1. function Area(selector){  
  2.     if(!Area.ALL_AREAS){  
  3.         throw new Error('areas not init!');  
  4.     }  
  5.   
  6.     this.selector = selector;  
  7.     var $province = this._getElement(Area.type.PROVINCE);  
  8.     var $city = this._getElement(Area.type.CITY);  
  9.     var $county = this._getElement(Area.type.COUNTY);  
  10.       
  11.     var self = this;  
  12.     $province.change(function(){  
  13.         var province = this.value;  
  14.         $city.html(self._getOptions(province));  
  15.         var city = $city.val();  
  16.         $county.html(self._getOptions(city));  
  17.     });  
  18.     $city.change(function(){  
  19.         var city = this.value;  
  20.         $county.html(self._getOptions(city));  
  21.     });  
  22.     $province.html(self._getOptions());  
  23.     this.select();  
  24. }  
  25.   
  26. $.extend(Area, {  
  27.     ALL_AREAS : null,  
  28.     type : {  
  29.         PROVINCE : 0,  
  30.         CITY : 1,  
  31.         COUNTY : 2  
  32.     },  
  33.     init : function(url){  
  34.         if(Area.ALL_AREAS) return;  
  35.         Area.ALL_AREAS = $.parseJSON($.ajax({  
  36.             url : url,  
  37.             async : false  
  38.         }).responseText);  
  39.     },  
  40.     getAddressByCode : function(code){  
  41.         var codePath = Area._toCodePath(code);  
  42.         var address = [];  
  43.         var areas = Area.ALL_AREAS;  
  44.         for(var i=0; i<codePath.length; i++){  
  45.             var area = areas[codePath[i]];  
  46.             if(!area) break;  
  47.             address.push(Area._getAreaName(area));  
  48.             areas = area[Area._getAreaName(area)];  
  49.         }  
  50.         return address;  
  51.     },  
  52.     _toCodePath : function(code){  
  53.         if(code == nullreturn [];  
  54.   
  55.         var provinceCode = parseInt(code / 10000) * 10000;  
  56.         var cityCode = parseInt(code / 100) * 100;  
  57.           
  58.         if(provinceCode == code) return [ code ];  
  59.         if(cityCode == code) return [provinceCode, code];  
  60.         return [provinceCode, cityCode, code];  
  61.     },  
  62.     _getAreaName : function(area){  
  63.         if(typeof(area) == 'string'return area;  
  64.         for(var o in area){  
  65.             return o;  
  66.         }  
  67.     }  
  68. });  
  69.   
  70. Area.prototype = {  
  71.     _getElement : function(type){  
  72.         return $(this.selector).find('select').eq(type);  
  73.     },  
  74.       
  75.     select : function(code){  
  76.         var $province = this._getElement(Area.type.PROVINCE);  
  77.         var $city = this._getElement(Area.type.CITY);  
  78.         var $county = this._getElement(Area.type.COUNTY);  
  79.   
  80.         var codePath = Area._toCodePath(code);  
  81.         var province = codePath.length < 1 ? '' : codePath[0];   
  82.         $province.val(province);  
  83.         $city.html(this._getOptions(province));  
  84.   
  85.         var city = codePath.length < 2 ? '' : codePath[1];   
  86.         $city.val(city);  
  87.         $county.html(this._getOptions(city));  
  88.   
  89.         var county = codePath.length < 3 ? '' : codePath[2];   
  90.         $county.val(county);  
  91.     },  
  92.       
  93.     getAddress : function(){  
  94.         var $province = this._getElement(Area.type.PROVINCE);  
  95.         var $city = this._getElement(Area.type.CITY);  
  96.         var $county = this._getElement(Area.type.COUNTY);  
  97.           
  98.         if($county.val()) return $county.val();  
  99.         if($city.val()) return $city.val();  
  100.         if($province.val()) return $province.val();  
  101.         return null;  
  102.     },  
  103.       
  104.     _getAreas : function(code, superAreas){  
  105.         var area = superAreas[ code ];  
  106.         if(!area) return {};  
  107.         return area[Area._getAreaName(area)] || {};  
  108.     },  
  109.       
  110.     _getAreasByCode : function(code){  
  111.         var areas = Area.ALL_AREAS;  
  112.         var codePath = Area._toCodePath(code);  
  113.           
  114.         for(var i=0; i<codePath.length; i++){  
  115.             areas = this._getAreas(codePath[i], areas);  
  116.         }  
  117.         return areas;  
  118.     },  
  119.       
  120.     _getOrderAreas : function(code){  
  121.         var areas = this._getAreasByCode(code);  
  122.         var codes = [];  
  123.         for(var i in areas){  
  124.             codes.push(i);  
  125.         }  
  126.         return {  
  127.             codes : codes.sort(),  
  128.             areas : areas  
  129.         };  
  130.     },  
  131.       
  132.     _getOption : function(code, area){  
  133.         return '<option value="' + code + '">' + Area._getAreaName(area) + '</option>';  
  134.     },  
  135.       
  136.     _getOptions : function(code){  
  137.         var orderAreas = this._getOrderAreas(code);  
  138.         var codes = orderAreas.codes;  
  139.         var areas = orderAreas.areas;  
  140.         var options = '<option value="">请选择</option>';  
  141.           
  142.         for(var i=0; i<codes.length; i++){  
  143.             options += this._getOption(codes[i], areas[codes[i]]);  
  144.         }  
  145.         return options;  
  146.     }  
  147. };  


area.json格式为了节省空间,分别将区划代码、区划名称作为map的key。 
在创建select-options的时候再按照区划代码进行排序。 
Json代码    收藏代码
  1. {  
  2.     "450000" : {  
  3.         "广西壮族自治区" : {  
  4.             "450800" : {  
  5.                 "贵港市" : {  
  6.                     "450802" : "港北区",  
  7.                     "450801" : "市辖区"  
  8.                 }  
  9.             },  
  10.             "450500" : {  
  11.                 "北海市" : {  
  12.                     "450501" : "市辖区"  
  13.                 }  
  14.             }  
  15.         }  
  16.     },  
  17.     "140000" : {  
  18.         "山西省" : {  
  19.         }  
  20.     }     
  21. }  

格式转换工具: 
Java代码    收藏代码
  1. public class Test2 {  
  2. public static void main(String[] args) throws Exception {  
  3.     Workbook workbook = Workbook.getWorkbook(Test2.class.getResourceAsStream("行政区划代码(截至2009年12月31日).xls"));  
  4.     Sheet sheet = workbook.getSheet(0);  
  5.   
  6.     /** key=code, value=area, area is string or map */  
  7.     Map<String, Object> roots = new HashMap();  
  8.     /** key=name, value=children map */  
  9.     Map<String, Map> lastProvince = null;  
  10.     /** key=name, value=children map */  
  11.     Map<String, Map> lastCity = null;  
  12.     for (int i = 3; i < sheet.getRows(); i++) {  
  13.         String code = trimToNull(sheet.getCell(0, i).getContents());  
  14.         String name = trimToNull(sheet.getCell(1, i).getContents());  
  15.         if (code == null && name == nullcontinue;  
  16.   
  17.         if (isProvince(code)) {  
  18.             lastProvince = new HashMap();  
  19.             lastProvince.put(name, new HashMap());  
  20.             roots.put(code, lastProvince);  
  21.             continue;  
  22.         }  
  23.   
  24.         if (isCity(code)) {  
  25.             lastCity = new HashMap();  
  26.             lastCity.put(name, new HashMap());  
  27.             lastProvince.values().iterator().next().put(code, lastCity);  
  28.             continue;  
  29.         }  
  30.   
  31.         lastCity.values().iterator().next().put(code, name);  
  32.     }  
  33.     workbook.close();  
  34.     ObjectMapper mapper = new ObjectMapper();  
  35.     mapper.writeValue(new File("area2.json"), roots);  
  36. }  
  37.   
  38. static String trimToNull(String text) {  
  39.     if (text == nullreturn null;  
  40.     text = text.replaceAll("\\s| |\u00a0""");  
  41.     return text.length() == 0 ? null : text;  
  42. }  
  43.   
  44. static boolean isProvince(String code) {  
  45.     return code.endsWith("0000");  
  46. }  
  47.   
  48. static boolean isCity(String code) {  
  49.     return !isProvince(code) && code.endsWith("00");  
  50. }  
  51. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值