小编给大家分享一下如何解决layui 三级联动下拉框更新时回显的问题,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
最近接触layui,比较简洁美观的一款ui,但也有很多小坑,记录一下实现省市区下拉三级联动更新回显的问题
<%--监听省份选择 --%>
form.on('select(uprovinceId)', function(data){
initCityList('change');
});
这里面有个问题就是 选择的时候 如果 请选择 的value 是“” 空字符串,当选择从河北省变到请选择时不会触发这个监听事件
<script type="text/javaScript">
layui.use(['form'],function(){
var form = layui.form;
<%--监听省份选择 --%>
form.on('select(uprovinceId)', function(data){
initCityList('change');
});
<%--监听城市选择 --%>
form.on('select(ucityId)', function(data){
initDistrictList('change');
});
<%--监听修改提交 --%>
form.on('submit(update)', function(data){
updateAccount();
form.render();
return false; //阻止表单跳转。如果需要表单跳转,去掉这段即可。
});
});
<%-- 监听取消按钮 --%>
$(document).on("click",".cancel",function(){
layer.closeAll("page");
});
$(function(){
<%--初始化省份列表 --%>
initProvinceList('init');
initCityList('init');
initDistrictList('init');
});
<%--初始化省份列表 --%>
function initProvinceList(flag){
var provinceId = $("#oldProvinceId").val();
$("#uprovinceId").append("<option value='" + -1 +"'>" + '请选择' + "</option>");
$("#udistrictId").append("<option value='" + -1 + "'>" + '请选择' + "</option>");
$("#ucityId").append("<option value='" + -1 + "'>" + '请选择' + "</option>");
var url = "${ctx}/admin/getProvinceList.action?ranNum="+Math.random();
$.post(url,{},function(data){
var list = eval(data);
for (i = 0; i < list.length; i++) {
var name = list[i].province;
var id = list[i].id;
if(id == provinceId){
$("#uprovinceId").append("<option value='" + id + "' selected>" + name + "</option>");
}else{
$("#uprovinceId").append("<option value='" + id + "'>" + name + "</option>");
}
}
layui.use(['form'],function(){
var form = layui.form;
form.render();
});
});
}
<%--初始化城市列表 --%>
function initCityList(flag){
var provinceId = $("#uprovinceId").val();
var oldProvinceId = $("#oldProvinceId").val();
if(provinceId == -1 && flag == "init"){
provinceId = oldProvinceId;
}
var cityId = $("#oldCityId").val();
$("#ucityId").empty();
$("#ucityId").append("<option value='" + -1 + "'>" + '请选择' + "</option>");
$("#udistrictId").empty();
$("#udistrictId").append("<option value='" + -1 + "'>" + '请选择' + "</option>");
var url = "${ctx}/admin/getCityList.action?ranNum="+Math.random();
$.post(url,{provinceId:provinceId},function(data){
var list = eval(data);
alert(list.length+"list长度");
for (i = 0; i < list.length; i++) {
var name = list[i].city;
var id = list[i].id;
if(id == cityId){
$("#ucityId").append("<option value='" + id + "' selected>" + name + "</option>");
}else{
$("#ucityId").append("<option value='" + id + "'>" + name + "</option>");
}
}
layui.use(['form'],function(){
var form = layui.form;
form.render();
});
});
}
<%--初始化区列表 --%>
function initDistrictList(flag){
var cityId = $("#ucityId").val();
var oldCityId = $("#oldCityId").val();
if(cityId == -1 && flag == "init"){
cityId = oldCityId;
}
var districtId = $("#oldDistrictId").val();
$("#udistrictId").empty();
$("#udistrictId").append("<option value='" + -1 + "'>" + '请选择' + "</option>");
var url = "${ctx}/admin/getDistrictList.action?ranNum="+Math.random();
$.post(url,{cityId:cityId},function(data){
var list = eval(data);
for (i = 0; i < list.length; i++) {
var name = list[i].district;
var id = list[i].id;
if( id == districtId){
$("#udistrictId").append("<option value='" + id + "' selected>" + name + "</option>");
}else{
$("#udistrictId").append("<option value='" + id + "'>" + name + "</option>");
}
}
layui.use(['form'],function(){
var form = layui.form;
form.render();
});
});
}
</script>
<div class="layui-form-item city" >
<label class="layui-form-label">所负责区域</label>
<div class="layui-inline">
<div class="layui-input-inline" >
<select id="uprovinceId" name="provinceId" lay-verify="required" lay-search="" lay-filter="uprovinceId">
<!-- <option value="-1">请选择</option> -->
</select>
</div>
</div>
<div class="layui-inline">
<div class="layui-input-inline" >
<select id="ucityId" name="cityId" lay-verify="required" lay-search="" lay-filter="ucityId" οnchange="initDistrictList()">
<!-- <option value="-1">请选择</option> -->
</select>
</div>
</div>
<div class="layui-inline">
<div class="layui-input-inline" >
<select id="udistrictId" name="districtId" lay-verify="required" lay-search="">
<!-- <option value="-1">请选择</option> -->
</select>
</div>
</div>
</div>
<input type = "hidden" value="${admin.provinceId }" id="oldProvinceId">
<input type = "hidden" value="${admin.cityId }" id="oldCityId">
<input type = "hidden" value="${admin.districtId }" id="oldDistrictId">
以上是“如何解决layui 三级联动下拉框更新时回显的问题”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注天达云行业资讯频道!