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

因为要读取本地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

最新文章

  1. 简记用ArcGIS处理某项目需求中数据的步骤
  2. jQ图片列表光标移动动画
  3. [C++][数据结构][算法]单链式结构的深拷贝
  4. POJ 1390 Block
  5. OracleBulkCopy
  6. JVM监控与调优
  7. UC脱茧蜕变,移动资讯市场格局再生变
  8. 拖拽碰撞效果,高级浏览器下全部搞定(ie6-8还没有搞定)
  9. web标准常见问题整理
  10. localtime和localtime_r
  11. openGL的使用步骤
  12. jQuery获取元素的兄弟节点的几种方法
  13. linux 中的搜索命令
  14. bzoj 4345: [POI2016]Korale
  15. laravel 定义翻译字符串
  16. service注入失败
  17. RabbitMQ基本操作
  18. Visualbox与CentOS 6.4之间鼠标切换
  19. node服务器中打开html文件的两种方法
  20. 机器学习进阶-阈值与平滑-图像平滑操作(去噪操作) 1. cv2.blur(均值滤波) 2.cv2.boxfilter(方框滤波) 3. cv2.Guassiannblur(进行高斯滤波) 4. cv2.medianBlur(进行中值滤波)

热门文章

  1. 解决 Intellij IDEA Cannot Resolve Symbol ‘BASE Decoder’ 问题
  2. 初识MySQL &lt;一&gt;
  3. UDP协议&amp;socketserver模块
  4. css 超出部分显示省略号 汇总
  5. 初探CSS -3 语法
  6. 缺少库libresolv
  7. C/C++ 零碎知识点
  8. 一、Signalr WebApi客服-客户链接+Redis
  9. redis数据库到mysql或mongodb数据库
  10. java 集合之HashMap、Hashtable、LinkedHashMap、TreeMap