之前看的省市区街道四级联动的插件,感觉和公司要求的有些不符合,就自己写了一个

因为要读取本地json文件,所以要跑下服务器

下载browser-sync

然后在项目目录下运行browser-sync start --server --file "**"

html

    <div>
<div class="field">
地址
</div>
<div id="area" class="field-value">
<p>
<select name="province">
<option value=""> - 请选择 - </option>
</select>
<select name="city" disabled>
<option value=""> - 请选择 - </option>
</select>
<select name="area" disabled>
<option value=""> - 请选择 - </option>
</select>
<select name="town" disabled>
<option value=""> - 请选择 - </option>
</select>
</p>
<!-- <p id="place">请选择地区</p> -->
</div>
</div>

css

        .field-value {
float: left;
}
.field-value select {
width: 115.5px;
height: 35px;
border: 1px solid #dddddd;
font-size: 14px;
} .field-value option {
width: 117.5px;
height: 35px;
}

js

$(document).ready(function () {
var $this = $("#area");
var $province = $this.find('select[name="province"]'),
$city = $this.find('select[name="city"]'),
$area = $this.find('select[name="area"]'),
$town = $this.find('select[name="town"]');
$.getJSON("./js/city.json", function (result) {
console.log(result)
if (result.rc == 0) {
var formatProvince = [], provinceField = [], currentProvince = null, cityField = [], currentCity = null, areaField = [], currentArea = null, townField = [];
formatProvince = result.data.province_list;
formatProvince.map(function (value, index) {
provinceField.push(value.area_name);
})
var format = {
province: function () {
$province.empty();
$province.append('<option value=""> - 请选择 - </option>');
$province.prop('disabled', provinceField.length === 0);
for (var i in provinceField) {
$province.append('<option value="' + i + '" data-code="' + i + '">' + provinceField[i] + '</option>');
}
this.city();
},
city: function () {
$city.empty();
$city.append('<option value=""> - 请选择 - </option>');
$city.prop('disabled', cityField.length === 0);
for (var i in cityField) {
$city.append('<option value="' + i + '" data-code="' + i + '">' + cityField[i] + '</option>');
}
this.area();
},
area: function () {
$area.empty();
$area.append('<option value=""> - 请选择 - </option>');
$area.prop('disabled', areaField.length === 0);
for (var i in areaField) {
$area.append('<option value="' + i + '" data-code="' + i + '">' + areaField[i] + '</option>');
}
this.town();
},
town: function () {
$town.empty();
$town.append('<option value=""> - 请选择 - </option>');
$town.prop('disabled', townField.length === 0);
for (var i in townField) {
$town.append('<option value="' + i + '" data-code="' + i + '">' + townField[i] + '</option>');
}
} }; format.province();
//事件绑定
$province.on('change', function () {
currentProvince = null, cityField = [], cityId = 0, currentCity = null, areaField = [], areaId = 0, currentArea = null, townField = [], townId = 0;
currentProvince = formatProvince[$province.find('option:selected').attr('data-code')]
provinceId = currentProvince.area_id;
// cityId = currentProvince.city_list[0].area_id;
currentProvince.city_list.map(function (value, index) {
cityField.push(value.area_name);
});
format.city();
});
$city.on('change', function () {
currentCity = null, areaField = [], areaId = 0, currentArea = null, townField = [], townId = 0;
currentCity = currentProvince.city_list[$city.find('option:selected').attr('data-code')]
cityId = currentCity.area_id;
// areaId=currentCity.county_list[0].area_id;
currentCity.county_list.map(function (value, index) {
areaField.push(value.area_name);
});
format.area();
});
$area.on('change', function () {
areaId = 0, currentArea = null, townField = [], townId = 0;
currentArea = currentCity.county_list[$area.find('option:selected').attr('data-code')]
areaId = currentArea.area_id;
// townId =currentArea.town_list[0].area_id;
currentArea.town_list.map(function (value, index) {
townField.push(value.area_name);
});
format.town();
// console.log(townField)
});
$town.on('change', function () {
townId = currentArea.town_list[$town.find('option:selected').attr('data-code')].area_id;
}) }
})
});

运行结果

github代码:https://github.com/ouxiaojie18/-/tree/master/%E7%9C%81%E5%B8%82%E5%8C%BA%E8%A1%97%E9%81%93%E5%9B%9B%E7%BA%A7%E8%81%94%E5%8A%A8

jq完成省市区街道四级联动的更多相关文章

  1. vue mint-ui 实现省市区街道4级联动(仿淘宝京东收货地址4级联动)

    demo及源码地址 https://github.com/artiely/citypicker 先去下载一个“省份.城市.区县.乡镇” 四级联动数据,然后 引入 import { Picker } f ...

  2. JQuery实现省市区的三级联动

    JQuery实现省市区的三级联动 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "h ...

  3. JavaScript实现省市区的三级联动

    JavaScript实现省市区的三级联动 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" &qu ...

  4. 省市区(县)三级联动代码(js 数据源)

    ylbtech-JavaScript-Utility:省市区(县)三级联动代码(js 数据源) 省市区(县)三级联动代码(js 数据源) 1.A,源代码(Source Code)返回顶部 1.A.1, ...

  5. C# 使用winForm的TreeView显示中国城镇四级联动

    直接上代码吧,这里 MySql.Data.MySqlClient;需要到mysql官网下载mysql-connector-net-6.9.8-noinstall.zip   访问密码 6073 usi ...

  6. 中国省市区地址三级联动jQuery插件 案例下载

    中国省市区地址三级联动jQuery插件 案例下载 distpicker 是一款可以实现中国省市区地址三级联动jQuery插件.它使用简单,简单设置即可完成中国省市区地址联动效果. 安装 可以通过npm ...

  7. js之省市区(县)三级联动效果

    省市区(县)三级联动效果,是我们软件开发比较常用的,特别是对一些crm,erp之类,当然也包括其他的后台管理系统,基本都涉及到,今天贴出这个常用的,方便个人复用和大家使用 <!DOCTYPE h ...

  8. 中国省市区地址三级联动插件---jQuery Distpicker

    插件描述:distpicker是一款可以实现中国省市区地址三级联动jQuery插件.它使用简单,简单设置即可完成中国省市区地址联动效果. [官网]https://fengyuanchen.github ...

  9. discuz 自带的地区四级联动调用方法

    首先,DZ提供了专门处理地区信息的函数,在source/function/function_profile.php(第14行)文件中:function profile_setting(){}那么,我们 ...

随机推荐

  1. 数组遍历方法forEach 和 map 的区别

    数组遍历方法forEach 和 map 的区别:https://www.cnblogs.com/sticktong/p/7602783.html

  2. mysql 聚合函数(2)

    平均 svg select avg(sal + IFNULL(comm,0)) as avg_sal from t_emp 总和 sum select sum(sal + IFNULL(comm,0) ...

  3. python with hadoop

    python with  hdfs hdfs 可以在 linux 本地操作 bin/hdfs dfs -ls /foo 但是这种只能在 命令行 操作. 通常我们需要在程序中实现远程操作,python ...

  4. [.net core]2.hello word(.net core web app模版简介)

    创建一个.net core web app project 弹出这个窗口 empty代表 最低依赖,  意味着往往需要手动按需添加依赖. web应用程序(模型视力控制器) 则会帮你创建好control ...

  5. 06.AutoMapper 之内联映射(Inline Mapping)

    https://www.jianshu.com/p/623655d7cb34 内联映射(Inline Mapping) AutoMapper在 6.2 以上版本将动态创建类型映射. 当第一次调用Map ...

  6. linux环境查看版本信息

    1.uname -a 2.cat /proc/version 3.cat /etc/issue 4.cat /proc/cpuinfo (查看cpu相关信息)

  7. 69. Sqrt(x) (JAVA)

    Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...

  8. VMware三种连接方式bridge, nat, host-only

    大家在安装完虚拟机后,默认安装了两个虚拟网卡,VMnet1和 VMnet8,其他的未安装(当然也可以手动安装其他的).其中VMnet1是host网卡,用于host方式连接网络的.VMnet8是NAT网 ...

  9. laravel5.8 eloquent

    https://learnku.com/docs/laravel/5.8/eloquent/3931 示例1: $flights = App\Flight::all(); foreach ($flig ...

  10. Python控制语句执行流程

    if语句: if<条件>:#条件之后必须有“:”.  <语句> if语句的三元运算符: <表达式1>if<条件>else<表达式2>:其语义 ...