下面是“js实现简单省市区三级选择联级”的完整攻略:
准备工作
- 首先,需要准备三个下拉框,用于选择省、市、区/县三级。
- 接着,需要准备好省市区/县的数据,可以从网络上搜集或是自己整理。常见的格式包括json, xml等。
HTML页面布局
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>省市区三级联动</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<div>
<label>省:</label>
<select id="province">
<option value="">请选择</option>
</select>
</div>
<div>
<label>市:</label>
<select id="city">
<option value="">请选择</option>
</select>
</div>
<div>
<label>区/县:</label>
<select id="area">
<option value="">请选择</option>
</select>
</div>
<script src="script.js"></script>
</body>
</html>
JS实现
- 使用 jQuery 的 ajax 方法异步获取省市区数据。示例代码如下:
// 异步获取省份信息
$.get('province.json', function(data) {
// 在console中打印一下数据,方便调试
console.log(data);
});
注:此处后端返回的数据格式是json,要注意与实际情况匹配。
- 解析省市区数据,并动态生成省、市、区/县列表。示例代码如下:
$.get('province.json', function(data) {
// 解析省急市区数据,然后动态添加到select控件中
var province = $('#province');
$.each(data, function(index, item) {
var option = $('<option>').val(item.code).text(item.name);
province.append(option);
});
// 为省添加change事件,选择不同的省时,加载相应的市信息
province.change(function() {
city.empty(); // 清空城市列表
area.empty(); // 清空区/县列表
var code = $(this).val();
if (!code) return; // 如果没有选择省份,则直接返回
// 异步获取城市信息
$.get('city.json', {province: code}, function(data) {
console.log(data);
$.each(data, function(index, item) {
var option = $('<option>').val(item.code).text(item.name);
city.append(option);
});
});
});
});
注:此处后端提供了根据省份code获取对应城市级别列表的接口地址,返回json数据格式,要注意与实际情况匹配。
- 当用户选择不同的城市时,根据城市的code加载对应的区/县列表。示例代码如下:
$.get('city.json', {province: code}, function(data) {
console.log(data);
$.each(data, function(index, item) {
var option = $('<option>').val(item.code).text(item.name);
city.append(option);
});
city.change(function() {
area.empty(); // 清空区县列表
var code = $(this).val();
if (!code) return; // 如果没有选择城市,则直接返回
// 异步获取区/县信息
$.get('area.json', {city: code}, function(data) {
console.log(data);
$.each(data, function(index, item) {
var option = $('<option>').val(item.code).text(item.name);
area.append(option);
});
});
});
});
注:此处后端提供了根据城市code获取对应区/县列表的接口地址,返回json数据格式,要注意与实际情况匹配。
完整示例
以下是一个完整的示例代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>省市区三级联动</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<div>
<label>省:</label>
<select id="province">
<option value="">请选择</option>
</select>
</div>
<div>
<label>市:</label>
<select id="city">
<option value="">请选择</option>
</select>
</div>
<div>
<label>区/县:</label>
<select id="area">
<option value="">请选择</option>
</select>
</div>
<script>
$(function() {
var province = $('#province');
var city = $('#city');
var area = $('#area');
// 异步获取省份信息
$.get('province.json', function(data) {
// 在console中打印一下数据,方便调试
console.log(data);
// 解析省急市区数据,然后动态添加到select控件中
$.each(data, function(index, item) {
var option = $('<option>').val(item.code).text(item.name);
province.append(option);
});
});
province.change(function() {
city.empty(); // 清空城市列表
area.empty(); // 清空区县列表
var code = $(this).val();
if (!code) return; // 如果没有选择省份,则直接返回
// 异步获取城市信息
$.get('city.json', {province: code}, function(data) {
console.log(data);
$.each(data, function(index, item) {
var option = $('<option>').val(item.code).text(item.name);
city.append(option);
});
});
});
city.change(function() {
area.empty(); // 清空区县列表
var code = $(this).val();
if (!code) return; // 如果没有选择城市,则直接返回
// 异步获取区县信息
$.get('area.json', {city: code}, function(data) {
console.log(data);
$.each(data, function(index, item) {
var option = $('<option>').val(item.code).text(item.name);
area.append(option);
});
});
});
});
</script>
</body>
</html>
同时,我们也可以使用mockjs快捷生成演示数据。例如下面示例提供的演示时省市区的js数据:
// mockjs数据:省份列表
Mock.mock('province.json', 'get', function() {
return [
{"name": "北京市", "code": "110000"},
{"name": "天津市", "code": "120000"},
{"name": "河北省", "code": "130000"},
{"name": "山西省", "code": "140000"},
{"name": "内蒙古自治区", "code": "150000"},
{"name": "辽宁省", "code": "210000"},
{"name": "吉林省", "code": "220000"},
{"name": "黑龙江省", "code": "230000"},
{"name": "上海市", "code": "310000"},
{"name": "江苏省", "code": "320000"},
{"name": "浙江省", "code": "330000"},
{"name": "安徽省", "code": "340000"},
{"name": "福建省", "code": "350000"},
{"name": "江西省", "code": "360000"},
{"name": "山东省", "code": "370000"},
{"name": "河南省", "code": "410000"},
{"name": "湖北省", "code": "420000"},
{"name": "湖南省", "code": "430000"},
{"name": "广东省", "code": "440000"},
{"name": "广西壮族自治区", "code": "450000"},
{"name": "海南省", "code": "460000"},
{"name": "重庆市", "code": "500000"},
{"name": "四川省", "code": "510000"},
{"name": "贵州省", "code": "520000"},
{"name": "云南省", "code": "530000"},
{"name": "西藏自治区", "code": "540000"},
{"name": "陕西省", "code": "610000"},
{"name": "甘肃省", "code": "620000"},
{"name": "青海省", "code": "630000"},
{"name": "宁夏回族自治区", "code": "640000"},
{"name": "新疆维吾尔自治区", "code": "650000"},
];
});
// mockjs数据:城市列表
Mock.mock('city.json', 'get', function(options) {
var province = options.body.match(/province=(\d+)/i);
var code = province[1];
var data = [];
if (code === '130000') {
data = [
{"name": "石家庄市", "code": "130100"},
{"name": "唐山市", "code": "130200"},
{"name": "秦皇岛市", "code": "130300"},
{"name": "邯郸市", "code": "130400"},
{"name": "邢台市", "code": "130500"},
{"name": "保定市", "code": "130600"},
{"name": "张家口市", "code": "130700"},
{"name": "承德市", "code": "130800"},
{"name": "沧州市", "code": "130900"},
{"name": "廊坊市", "code": "131000"},
{"name": "衡水市", "code": "131100"}
];
} else if (code === '320000') {
data = [
{"name": "南京市", "code": "320100"},
{"name": "无锡市", "code": "320200"},
{"name": "徐州市", "code": "320300"},
{"name": "常州市", "code": "320400"},
{"name": "苏州市", "code": "320500"},
{"name": "南通市", "code": "320600"},
{"name": "连云港市", "code": "320700"},
{"name": "淮安市", "code": "320800"},
{"name": "盐城市", "code": "320900"},
{"name": "扬州市", "code": "321000"},
{"name": "镇江市", "code": "321100"},
{"name": "泰州市", "code": "321200"},
{"name": "宿迁市", "code": "321300"}
];
} else if (code === '440000') {
data = [
{"name": "广州市", "code": "440100"},
{"name": "韶关市", "code": "440200"},
{"name": "深圳市", "code": "440300"},
{"name": "珠海市", "code": "440400"},
{"name": "汕头市", "code": "440500"},
{"name": "佛山市", "code": "440600"},
{"name": "江门市", "code": "440700"},
{"name": "湛江市", "code": "440800"},
{"name": "茂名市", "code": "440900"},
{"name": "肇庆市", "code": "441200"},
{"name": "惠州市", "code": "441300"},
{"name": "梅州市", "code": "441400"},
{"name": "汕尾市", "code": "441500"},
{"name": "河源市", "code": "441600"},
{"name": "阳江市", "code": "441700"},
{"name": "清远市", "code": "441800"},
{"name": "东莞市", "code": "441900"},
{"name": "中山市", "code": "442000"},
{"name": "潮州市", "code": "445100"},
{"name": "揭阳市", "code": "445200"},
{"name": "云浮市", "code": "445300"}
];
}
return data;
});
// mockjs数据:区县列表
Mock.mock('area.json', 'get', function(options) {
var city = options.body.match(/city=(\d+)/i);
var code = city[1];
var data = [];
if (code === '130100') {
data = [
{"name": "长安区", "code": "130102"},
{"name": "桥西区", "code": "130104"},
{"name": "新华区", "code": "130105"},
{"name": "井陉矿区", "code": "130107"},
{"name": "辛集市", "code": "130181"}
];
} else if (code === '320100') {
data = [
{"name": "玄武区", "code": "320102"},
{"name": "白下区", "code": "320103"},
{"name": "秦淮区", "code": "320104"},
{"name": "鼓楼区", "code": "320105"},
{"name": "浦口区", "code": "320111"},
{"name": "栖霞区", "code": "320113"},
{"name": "江宁区", "code": "320115"},
{"name": "雨花台区", "code": "320114"},
{"name": "溧水区", "code": "320117"}
];
} else if (code === '440100') {
data = [
{"name": "荔湾区", "code": "440103"},
{"name": "越秀区", "code": "440104"},
{"name": "海珠区", "code": "440105"},
{"name": "天河区", "code": "440106"}
];
}
return data;
});
以上就是实现省市区三级联动的详细攻略,如果有不清楚的地方欢迎提问。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:js实现简单省市区三级选择联级 - Python技术站