js layui下拉框二级联动
时间: 2023-07-08 16:04:07 浏览: 189
在 layui 中实现下拉框的二级联动可以使用官方提供的 `form` 模块和自定义的数据源。具体实现步骤如下:
1. 引入 `form` 模块和 `jquery` 库
```html
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/layui/2.5.7/layui.all.min.js"></script>
```
2. 在 HTML 中定义两个下拉框,其中第一个下拉框为省份,第二个下拉框为城市
```html
<div class="layui-form-item">
<label class="layui-form-label">省份</label>
<div class="layui-input-block">
<select name="province" id="province" lay-filter="province">
<option value="">请选择</option>
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">城市</label>
<div class="layui-input-block">
<select name="city" id="city" lay-filter="city">
<option value="">请选择</option>
</select>
</div>
</div>
```
3. 使用 `$.getJSON()` 方法获取数据源,并将数据渲染到第一个下拉框中
```javascript
$(function () {
$.getJSON("data.json", function (data) {
var provinceHtml = '<option value="">请选择</option>';
for (var i = 0; i < data.length; i++) {
provinceHtml += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
}
$("#province").html(provinceHtml);
layui.form.render();
});
});
```
4. 为第一个下拉框 `province` 绑定 `change` 事件,获取选中的省份 ID,然后根据 ID 获取对应的城市列表,并将数据渲染到第二个下拉框中
```javascript
layui.form.on("select(province)", function (data) {
var pid = data.value;
if (pid) {
$.getJSON("data.json", function (data) {
var cityHtml = '<option value="">请选择</option>';
for (var i = 0; i < data.length; i++) {
if (data[i].id == pid) {
for (var j = 0; j < data[i].cities.length; j++) {
cityHtml += '<option value="' + data[i].cities[j].id + '">' + data[i].cities[j].name + '</option>';
}
break;
}
}
$("#city").html(cityHtml);
layui.form.render();
});
} else {
$("#city").html('<option value="">请选择</option>');
layui.form.render();
}
});
```
5. 在 `data.json` 中定义数据源,格式如下:
```json
[
{
"id": "110000",
"name": "北京市",
"cities": [
{
"id": "110100",
"name": "市辖区"
},
{
"id": "110200",
"name": "县"
}
]
},
{
"id": "120000",
"name": "天津市",
"cities": [
{
"id": "120100",
"name": "市辖区"
},
{
"id": "120200",
"name": "县"
}
]
}
]
```
完整代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>layui下拉框二级联动</title>
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/layui/2.5.7/css/layui.min.css">
</head>
<body>
<div class="layui-container">
<form class="layui-form">
<div class="layui-form-item">
<label class="layui-form-label">省份</label>
<div class="layui-input-block">
<select name="province" id="province" lay-filter="province">
<option value="">请选择</option>
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">城市</label>
<div class="layui-input-block">
<select name="city" id="city" lay-filter="city">
<option value="">请选择</option>
</select>
</div>
</div>
</form>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/layui/2.5.7/layui.all.min.js"></script>
<script>
$(function () {
$.getJSON("data.json", function (data) {
var provinceHtml = '<option value="">请选择</option>';
for (var i = 0; i < data.length; i++) {
provinceHtml += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
}
$("#province").html(provinceHtml);
layui.form.render();
});
});
layui.form.on("select(province)", function (data) {
var pid = data.value;
if (pid) {
$.getJSON("data.json", function (data) {
var cityHtml = '<option value="">请选择</option>';
for (var i = 0; i < data.length; i++) {
if (data[i].id == pid) {
for (var j = 0; j < data[i].cities.length; j++) {
cityHtml += '<option value="' + data[i].cities[j].id + '">' + data[i].cities[j].name + '</option>';
}
break;
}
}
$("#city").html(cityHtml);
layui.form.render();
});
} else {
$("#city").html('<option value="">请选择</option>');
layui.form.render();
}
});
</script>
</body>
</html>
```
阅读全文