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

因为要读取本地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. 【6.28校内test】T3 【音乐会】道路千万条

    [音乐会]道路千万条[题目链接] 首先,你可以忽略上面的一大坨题面,只需要看说明的那一小部分就好啦. 然后理解题意: 就是说我们要给这n-1个运算符指定一个顺序,统计所有值为true的方案数pt,统计 ...

  2. c++中的四种智能指针

    c++中的四种智能指针 写惯了python,golang再来写c++总觉得头大,很大一个原因就是他没有一个GC机制. 不过c++中提供了智能指针,也不是不能用,李姐万岁! auto_ptr, shar ...

  3. PythonError解决方案

    # Pip Error pip install * error: Microsoft Visual C++ 14.0 is required. Get it with “Microsoft Visua ...

  4. volatile关键字?MESI协议?指令重排?内存屏障?这都是啥玩意

    一.摘要 三级缓存,MESI缓存一致性协议,指令重排,内存屏障,JMM,volatile.单拿一个出来,想必大家对这些概念应该有一定了解.但是这些东西有什么必然的联系,或者他们之间究竟有什么前世今生想 ...

  5. sql删除重复行和删除字段首位

    删除重复行 user_info: -- 单字段筛选重复行 SELECT *from user_info WHERE NAME in ( SELECT NAME from user_info GROUP ...

  6. wpf prism加载独立模块实例

    一.首先看看文件的组织架构 module1 module2生成dll某块.Shell来显示管理模块 二,看看关键bootstrapper类 using System;using System.Coll ...

  7. ubuntu安装selenium谷歌插件

    爬虫之selenium 安装与 chromedriver安装 今天学到一个有意思的插件,就是chromedriver,在爬虫的时候,如果网站反爬虫做的很好,自己又很想爬去里面的数据,那就可以用这个插件 ...

  8. LVS负载均衡常用的工作模式有NAT、DR、和TUN三种,其中DR模式性能最为优越,使用最为广泛。

    一.安装LVS LVS的编译安装参考本站文章:http://www.linuxe.cn/post-192.html,对于LVS这种功能性软件,在生产中用yum安装也是没有问题的. 1 yum inst ...

  9. php邮件防注入以及实现经典代码

    <?php function spamcheck($field) {     // filter_var() 过滤 e-mail     // 使用 FILTER_SANITIZE_EMAIL ...

  10. PHP5 构造函数

    在最近自己写的PHP小程序中遇到了如何使用PHP构造函数的情况,在PHP中允许我们在一个类中定义一个构造函数 如: <?php class User { public $name; functi ...