解决layui 三级联动下拉框更新时回显的问题
程序员文章站
2022-09-07 08:58:11
最近接触layui,比较简洁美观的一款ui,但也有很多小坑,记录一下实现省市区下拉三级联动更新回显的问题
<%--监听省份选择 --%>
f...
最近接触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" style="margin-bottom: -8px;"> <label class="layui-form-label">所负责区域</label> <div class="layui-inline"> <div class="layui-input-inline" style="width: 90px;margin-right: 0px;margin-bottom: 0px;"> <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" style="width: 90px;margin-right: 0px;margin-bottom: 0px;"> <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" style="width: 90px;margin-right: 0px;margin-bottom: 0px;"> <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 三级联动下拉框更新时回显的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。