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

因为要读取本地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. MyBatis一级缓存的笔记及记录

    精髓内容来源于<图灵学院> 一.概述: 一级缓存是MyBatis天然自带的,是默认开启且没有关闭的地方,1级缓存只能作用于查询回话中,所以也叫会话缓存: 这里举个例子: 订单表存在一对多的 ...

  2. fid解释

    VID就是VLAN ID,这个意思很明白.PVID就是PORT VID,当一个PORT属于多个VLAN时,当它收到不带TAG的数据时,它 就给数据加上TAG,其中VID=PVID.FID就是FILTE ...

  3. sql server 平方根函数SQRT(x)

    --SQRT(x)返回非负数x的二次方根 示例:select  SQRT(9), SQRT(36); 结果:3    6

  4. 启动tomcat出现org.springframework.web.servlet.DispatcherServlet错误

    项目右键 properites ==> deployment Assembly => add lib包

  5. JVM常用虚拟机命令汇总

    title: JVM常用虚拟机命令汇总 comments: false date: 2019-07-22 11:45:33 description: 总结一下常用的JVM虚拟机启动命令. catego ...

  6. 配置ShiroFilter需要注意的问题(Shiro_DelegatingFilterProxy)

    ShiroFilter的工作原理 ShiroFilter:DelegatingFilterProxy作用是自动到Spring 容器查找名字为shiroFilter(filter-name)的bean并 ...

  7. tomcat中部署多个项目,webAppRootKey 参数配置

    在一个tomcat中部署多个项目时,需要在每个项目的web.xml中配置webAppRootKey参数,如下: <context-param> <param-name>webA ...

  8. 再谈变分自编码器VAE:从贝叶斯观点出发

    链接:https://kexue.fm/archives/5343

  9. Java并发编程实战 第11章 性能与可伸缩性

    关于性能 性能的衡量标准有很多,如: 服务时间,等待时间用来衡量程序的"运行速度""多快". 吞吐量,生产量用于衡量程序的"处理能力",能够 ...

  10. java 集合之HashMap、Hashtable、LinkedHashMap、TreeMap

    HashMap 实现了Map接口,线程不安全. 实现原理: HashMap由数组+链表组成,数组是HashMap的主体,链表则是主要为了解决哈希冲突而存在的. 如果通过hash定位到数组位置没有链表, ...