SoFunction
Updated on 2025-03-06

Data echo method based on layui drop-down list

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");
      }
    });
  })
&lt;/script&gt;

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.

&lt;div class="layui-input-inline"&gt;
      &lt;select  name="quiz1" lay-filter="quiz1"&gt;
        &lt;option value="1"&gt;Please select the first-level service directory&lt;/option&gt;
        &lt;option th:each="list1:${typeList1}" th:value="${}" th:text="${ }"&gt;&lt;/option&gt;
      &lt;/select&gt;
    &lt;/div&gt;
    &lt;div class="layui-input-inline" lay-filter="inline1"&gt;
      &lt;select  name="quiz2" lay-filter="quiz2"&gt;
        &lt;option value="0"&gt;Please select the secondary service directory&lt;/option&gt;
      &lt;/select&gt;
    &lt;/div&gt;

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 = $('&lt;option value="' + msg[i].typeId + '"&gt;' + msg[i].typeName + '&lt;/option&gt;');
          $('#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 = $('&lt;option value="' + msg[i].typeId + '" selected="selected"&gt;' + msg[i].typeName + '&lt;/option&gt;');
                $('#quiz2').append($content1);
              } else {
                var $content = $('&lt;option value="' + msg[i].typeId + '"&gt;' + msg[i].typeName + '&lt;/option&gt;');
                $('#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.