jq完成省市区街道四级联动
2024-08-31 15:40:44
之前看的省市区街道四级联动的插件,感觉和公司要求的有些不符合,就自己写了一个
因为要读取本地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;
}) }
})
});
运行结果
最新文章
- 简记用ArcGIS处理某项目需求中数据的步骤
- jQ图片列表光标移动动画
- [C++][数据结构][算法]单链式结构的深拷贝
- POJ 1390 Block
- OracleBulkCopy
- JVM监控与调优
- UC脱茧蜕变,移动资讯市场格局再生变
- 拖拽碰撞效果,高级浏览器下全部搞定(ie6-8还没有搞定)
- web标准常见问题整理
- localtime和localtime_r
- openGL的使用步骤
- jQuery获取元素的兄弟节点的几种方法
- linux 中的搜索命令
- bzoj 4345: [POI2016]Korale
- laravel 定义翻译字符串
- service注入失败
- RabbitMQ基本操作
- Visualbox与CentOS 6.4之间鼠标切换
- node服务器中打开html文件的两种方法
- 机器学习进阶-阈值与平滑-图像平滑操作(去噪操作) 1. cv2.blur(均值滤波) 2.cv2.boxfilter(方框滤波) 3. cv2.Guassiannblur(进行高斯滤波) 4. cv2.medianBlur(进行中值滤波)
热门文章
- 解决 Intellij IDEA Cannot Resolve Symbol ‘BASE Decoder’ 问题
- 初识MySQL <;一>;
- UDP协议&;socketserver模块
- css 超出部分显示省略号 汇总
- 初探CSS -3 语法
- 缺少库libresolv
- C/C++ 零碎知识点
- 一、Signalr WebApi客服-客户链接+Redis
- redis数据库到mysql或mongodb数据库
- java 集合之HashMap、Hashtable、LinkedHashMap、TreeMap