layui.use(['form', 'upload', 'layer'], function () {
var form = layui.form;
//检查项目添加到下拉框中
$.ajax({
url: '/User/GetLevelThree',
dataType: 'json',
data: { "action_post": "GetProvince" },
type: 'POST',
success: function (data) {
var jsons = $.parseJSON(data);
$.each(jsons, function (index, item) {
$('#Province').append(new Option(item.name, item.code));// 下拉菜单里添加元素
});
layui.form.render('select', 'Province');
}
})
form.on('select(Province)', function (data) {
$.ajax({
type: "POST",
url: "/User/GetLevelThree",
data: { "action_post": "GetCity", "provinceid": data.value },
async: false,
success: function (datas) {
var jsons = $.parseJSON(datas);
$("#city").empty();
$.each(jsons, function (index, item) {
$('#city').append(new Option(item.name, item.code));// 下拉菜单里添加元素
});
form.render('select');
}
})
});
form.on('select(city)', function (data) {
$.ajax({
type: "POST",
url: "/User/GetLevelThree",
data: { "action_post": "GetCounty", "cityid": data.value },
async: false,
success: function (datas) {
var jsons = $.parseJSON(datas);
$("#county").empty();
$.each(jsons, function (index, item) {
$('#county').append(new Option(item.name, item.code));// 下拉菜单里添加元素
});
form.render('select');
}
})
console.log(data.elem); //得到select原始DOM对象
console.log(data.value); //得到被选中的值
console.log(data.othis); //得到美化后的DOM对象
});
});
<label class="layui-form-label">省份:</label>
<div class="layui-input-inline" style="width:100px">
<select class="layui-select" name="Province" lay-verify="Province" id="Province" lay-filter="Province">
<option>请选择</option>
</select>
</div>
<label class="layui-form-label">市区:</label>
<div class="layui-input-inline" style="width:100px">
<select class="layui-select" name="city" lay-verify="city" id="city" lay-filter="city">
<option>请选择</option>
</select>
</div>
<label class="layui-form-label">区/县:</label>
<div class="layui-input-inline" style="width:100px">
<select class="layui-select" name="county" lay-verify="county" id="county" lay-filter="county">
<option>请选择</option>
</select>
</div>
省市县三级联动下拉框
本文介绍了一个基于layui框架实现的省市县三级联动下拉菜单的JavaScript代码示例。通过Ajax请求获取省市区数据,并在用户选择省或市时动态更新下级菜单选项。此代码适用于需要地区选择功能的表单。
5808

被折叠的 条评论
为什么被折叠?



