基于jquery实现省市区三级联动效果

本文实现更新了项目的省市区三级联动数据,更新后最新的海南三沙都有,分享给所有需要的小伙伴们。
JQUERY + JSON,无数据库,纯JS代码,无加密,无压缩,可直接使用在任何项目中。

说明:数据来源于国家统计局官网。

先上图:

基于jquery实现省市区三级联动效果

绑定省市区

基于jquery实现省市区三级联动效果

使用方法:

1. 引用JQUERY   

<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>

2. 引用省市区数据

<script type="text/javascript" src="https://www.jb51.net/pdata.js"></script>

3. HTML代码:

<div> <div> <div> <label>所在区域</label> <div> <select> </select> </div> <div> <select> </select> </div> <div> <select> </select> </div> </div> </div> </div>

4. JS代码:

$(function () { var html = "<option value=''>== 请选择 ==</option>"; $("#input_city").append(html); $("#input_area").append(html); $.each(pdata,function(idx,item){ if (parseInt(item.level) == 0) { html += "<option value='" + item.names + "' exid='" + item.code + "'>" + item.names + "</option>"; } }); $("#input_province").append(html); $("#input_province").change(function(){ if ($(this).val() == "") return; $("#input_city option").remove(); $("#input_area option").remove(); var code = $(this).find("option:selected").attr("exid"); code = code.substring(0,2); var html = "<option value=''>== 请选择 ==</option>"; $("#input_area").append(html); $.each(pdata,function(idx,item){ if (parseInt(item.level) == 1 && code == item.code.substring(0,2)) { html += "<option value='" + item.names + "' exid='" + item.code + "'>" + item.names + "</option>"; } }); $("#input_city").append(html); }); $("#input_city").change(function(){ if ($(this).val() == "") return; $("#input_area option").remove(); var code = $(this).find("option:selected").attr("exid"); code = code.substring(0,4); var html = "<option value=''>== 请选择 ==</option>"; $.each(pdata,function(idx,item){ if (parseInt(item.level) == 2 && code == item.code.substring(0,4)) { html += "<option value='" + item.names + "' exid='" + item.code + "'>" + item.names + "</option>"; } }); $("#input_area").append(html); }); //绑定 $("#input_province").val("广东省");$("#input_province").change(); $("#input_city").val("深圳市");$("#input_city").change(); $("#input_area").val("罗湖区"); });

源码下载: 《基于jquery实现省市区三级联动效果》

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgwsxx.html