Static web page + layui rendering
html code
<div class="layui-form-item"> <label class="layui-form-label">Select box</label> <div class="layui-input-block"> <select name="quiz2"> <option value="">Please select a city</option> <option value="2">Hangzhou2</option> <option value="3">Hangzhou3</option> <option value="4">Hangzhou4</option> <option value="5">Hangzhou5</option> </select> </div> </div>
js code
<script> // traversal select $("#t").each(function() { // This represents <option></option>, and then traverse the option $(this).children("option").each(function() { // Determine which option needs to be echoed if ( == 2) { ($(this).text()); // Echo $(this).attr("selected","selected"); } }); }) </script>
Dynamic web page + layui rendering (cascading drop-down list)
The background transfers the data from the first drop-down list to the front desk
public String getApiInfoByTypePage(@PathVariable String type, Model model, HttpServletRequest request) { List<DopApiType> typeList1 = (1); (typeList1); ("typeList1", typeList1); }
The front-end page uses Thymeleaf traversal to load the data, and then layui automatically renders it.
<div class="layui-input-inline"> <select name="quiz1" lay-filter="quiz1"> <option value="1">Please select the first-level service directory</option> <option th:each="list1:${typeList1}" th:value="${}" th:text="${ }"></option> </select> </div> <div class="layui-input-inline" lay-filter="inline1"> <select name="quiz2" lay-filter="quiz2"> <option value="0">Please select the secondary service directory</option> </select> </div>
Listening events for first-level drop-down list
//Select time of listening to the drop-down list of first-level service directory ('select(quiz1)', function (data) { var pId = ;// id of the first-level list $.post('/getApiTypeByPid', {'pId': pId, '': 2}, function (msg) {// Request data for the secondary list // (msg); $('#quiz2').empty();// Clear the contents of the secondary list for (var i in msg) {// traversal data assigned to the secondary list var $content = $('<option value="' + msg[i].typeId + '">' + msg[i].typeName + '</option>'); $('#quiz2').append($content); } ('select');// Note: After the data assignment is completed, this method must be called to render the layout, otherwise the data will not be released. }); });
The core logic of data echo (java is written by itself according to the directory format)
js part
// Data echo in the service directory var sesType = [[${type}]]; var sesType1 = [[${type1}]];// Level 1 directory id var sesStatus = [[${status}]]; // Level 1 directory echo $("#quiz1").each(function () {// traversal select $(this).children("option").each(function () {// traversal option if ( == sesType1) {// The same id as the background, the selected displays // ("First-level directory" + $(this).text()); $(this).attr("selected", "selected"); $.post('/getApiTypeByPid', {'pId': sesType1, '': 2}, function (msg) {// Obtain information of the secondary directory based on the id of the primary directory // (msg); $('#quiz2').empty();// Clear for (var i in msg) { // traversal and assign values if (msg[i].typeId == sesType) {//Judge the secondary directory to be echoed var $content1 = $('<option value="' + msg[i].typeId + '" selected="selected">' + msg[i].typeName + '</option>'); $('#quiz2').append($content1); } else { var $content = $('<option value="' + msg[i].typeId + '">' + msg[i].typeName + '</option>'); $('#quiz2').append($content); } } ('select');// Note: Be sure to call this method for central rendering, otherwise the data will not be displayed. }); } }); });
The above data echo method based on the Layui drop-down list is all the content I share with you. I hope you can give you a reference and I hope you can support me more.