jQuery读取json文件,实现省市区/县(国标)三级联动
最近做一个微信项目,需要用户填写所在的省市区/县,决定使用jQuery读取json文件来实现省市区/县的联动。
其实很简单,jQuery文档也有详细解释:

代码如下:
html
例子截图:

其实很简单,jQuery文档也有详细解释:

代码如下:
html
- <table width="100%" border="0" cellspacing="0" cellpadding="0">
- <tr>
- <td>
- <!-- 暂无 ** 如需要可在city.json文件中添加-->
- <select id="province" name="province" onchange="doProvAndCityRelation();">
- <option id="choosePro" value="-1">请选择您所在省份</option>
- </select>
- </td>
- </tr>
- <tr>
- <td>
- <select id="citys" name="city" onchange="doCityAndCountyRelation();">
- <option id='chooseCity' value='-1'>请选择您所在城市</option>
- </select>
- </td>
- </tr>
- <tr>
- <td>
- <select id="county" name="county">
- <option id='chooseCounty' value='-1'>请选择您所在区/县</option>
- </select>
- </td>
- </tr>
- </table>
- <script language="javascript">
- var cityJson;
- $(function(){
- //load city.json
- $.getJSON(
- "city.json",
- function(obj) {
- cityJson = obj;
- var sb = new StringBuffer();
- $.each(obj, function(i, val) {
- if(val.item_code.substr(2, 4) == '0000'){
- sb.append("<option value='"+val.item_code+"'>"+val.item_name+"</option>");
- }
- });
- $("#choosePro").after(sb.toString());
- }
- );
- });
- // 省值变化时 处理市
- function doProvAndCityRelation(){
- var city = $("#citys");
- var county = $("#county");
- if(city.children().length > 1){
- city.empty();
- }
- if(county.children().length > 1){
- county.empty();
- }
- if($("#chooseCity").length == 0){
- city.append("<option id='chooseCity' value='-1'>请选择您所在城市</option>");
- }
- if($("#chooseCounty").length == 0){
- county.append("<option id='chooseCounty' value='-1'>请选择您所在区/县</option>");
- }
- var sb = new StringBuffer();
- $.each(cityJson, function(i, val) {
- if(val.item_code.substr(0, 2) == $("#province").val().substr(0, 2) && val.item_code.substr(2, 4) != '0000' && val.item_code.substr(4, 2) == '00'){
- sb.append("<option value='"+val.item_code+"'>"+val.item_name+"</option>");
- }
- });
- $("#chooseCity").after(sb.toString());
- }
- // 市值变化时 处理区/县
- function doCityAndCountyRelation(){
- var cityVal = $("#citys").val();
- var county = $("#county");
- if(county.children().length > 1){
- county.empty();
- }
- if($("#chooseCounty").length == 0){
- county.append("<option id='chooseCounty' value='-1'>请选择您所在区/县</option>");
- }
- var sb = new StringBuffer();
- $.each(cityJson, function(i, val) {
- if(cityVal=='110100' || cityVal=="120100" || cityVal=="310100" || cityVal=="500100"){
- if(val.item_code.substr(0, 3) == cityVal.substr(0, 3) && val.item_code.substr(4, 2) != '00'){
- sb.append("<option value='"+val.item_code+"'>"+val.item_name+"</option>");
- }
- }else{
- if(val.item_code.substr(0, 4) == cityVal.substr(0, 4) && val.item_code.substr(4, 2) != '00'){
- sb.append("<option value='"+val.item_code+"'>"+val.item_name+"</option>");
- }
- }
- });
- $("#chooseCounty").after(sb.toString());
-
- }
-
- function StringBuffer(str){
- var arr = [];
- str = str || "";
- var size = 0 ; // 存放数组大小
- arr.push(str);
- // 追加字符串
- this.append = function(str1) {
- arr.push(str1);
- return this;
- };
- // 返回字符串
- this.toString = function(){
- return arr.join("");
- };
- // 清空
- this.clear = function(key){
- size = 0 ;
- arr = [] ;
- }
- // 返回数组大小
- this.size = function(){
- return size ;
- }
- // 返回数组
- this.toArray = function(){
- return buffer ;
- }
- // 倒序返回字符串
- this.doReverse = function(){
- var str = buffer.join('') ;
- str = str.split('');
- return str.reverse().join('');
- }
- };
- </script>
- [
- {"item_code":"110000","item_name":"北京市"},
- {"item_code":"120000","item_name":"天津市"},
- {"item_code":"130000","item_name":"河北省"},
- {"item_code":"140000","item_name":"山西省"},
例子截图:

【jQuery读取json文件,实现省市区/县(国标)三级联动】相关文章
1. jQuery读取json文件,实现省市区/县(国标)三级联动
5. Telefónica与Mozilla开拓基于HTML5互联网设备
7. jQuery 1.7将HTML5引入IE6-8,实施弃用策略
8. jQuery 1.7将HTML5引入IE6-8,实施弃用策略
本文来源:https://www.51html5.com/a1340.html
﹝jQuery读取json文件,实现省市区/县(国标)三级联动﹞相关内容
- jQuery 人脸识别插件,支持图片和视频
- jQuery 浮动标签插件,帮助你提升表单用户体验
- CSS3+jQuery实现文件夹选项卡
- CSS3+jQuery实现文件夹选项卡
- 国标麻将
- UC优视加入W3C并参与HTML5国标制定讨论
- SOSO地图触屏版灰度发布:HTML5开发跨平台
- 弥补Web开发缺陷,实战HTML5中存储API
- 带进度条的jQuery文件拖放上传插件
- 速度非常快的 Web 服务器软件,Monkey HTTP Server v1.5.6 发布