1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| $(function () { layui.use(['jquery', 'form'], function () { let form = layui.form; form.on('select(province)', function (data) { selectRegion("#province", [ {element: "#city", text: "请选择市"}, {element: "#county", text: "请选择县"}, {element: "#town", text: "请选择镇"}, {element: "#village", text: "请选择村"}, ], data, form); }); form.on('select(city)', function (data) { selectRegion("#city", [ {element: "#county", text: "请选择县"}, {element: "#town", text: "请选择镇"}, {element: "#village", text: "请选择村"}, ], data, form); }); form.on('select(county)', function (data) { selectRegion("#county", [ {element: "#town", text: "请选择镇"}, {element: "#village", text: "请选择村"}, ], data, form); }); form.on('select(town)', function (data) { selectRegion("#town", [ {element: "#village", text: "请选择村"}, ], data, form) }); }); });
function selectRegion(element, childElements, data, form) { let regionId = data.value; $.ajax({ url: "${request.contextPath}/select", type: "get", dataType: "json", data: { "regionId": regionId }, success: function (data) { for (let i = 0; i < childElements.length; i++) { <!--清空下拉框中的缓存--> $(childElements[i].element).empty(); <!--避免下拉框的值不变--> $(childElements[i].element).append('<option disabled selected value="">' + childElements[i].text + "</option>"); } <!--使用循环解析后端传来的数据,并用使用js动态拼接html语言--> if (regionId !== "") { for (let i = 0; i < data.length; i++) { $(childElements[0].element).append("<option value='" + data[i].uuid + "'>" + data[i].name + "</option>"); } } form.render(); } }); }
|