Home > Web Front-end > Layui Tutorial > How to set select items in layui

How to set select items in layui

藏色散人
Release: 2020-12-17 10:18:13
Original
7717 people have browsed it

The method of setting the select item in layui: first create an HTML sample file; then request the select elements from the background through ajax; finally, click on a certain dd element under dl to realize the select selection Events are enough.

How to set select items in layui

The operating environment of this tutorial: Windows 7 system, layui version 1.0. This method is suitable for all brands of computers.

Recommended: "javascript basic tutorial""layUI tutorial"

1. Demand scenario analysis

The user needs to change something Information about an activity. The activity information contains field A that needs to be selected using the drop-down box. When the user clicks to modify the pop-up dialog box, the existing activity information needs to be filled in, and the existing value of field A is automatically selected.

2. How to implement Layui's select drop-down box

In order to obtain a select drop-down box that comes standard, we need to fill in the content in the html as follows:

<div class="layui-form-item">
 <label class="layui-form-label"><span class="color-red">* </span>发送对象:</label>
 <div class="layui-input-inline">
  <select id="edit_exam_school">
   <option value="">请选择</option>
   <option value="1">华南理工大学大学城</option>
   <option value="2">华南理工大学五山校区</option>
   <option value="3">中山大学珠海校区</option>
   <option value="4">中山大学大学城校区</option>
  </select>
 </div>
</div>
Copy after login

What is the rendering result of layui on the above html?

How to set select items in layui

The elements available for selection here are obtained from the background request through ajax, and which one to select needs to be decided based on the dynamic results. Analyze the structure of the rendering result and get the dom tree as follows:

How to set select items in layui

#It is found that in addition to select under layui-input-inline, there is another layui-form-select. div. This div contains two child elements, layui-select-title and dl. The select event of select can be achieved by clicking on a certain dd element under dl.

3. How to implement automatic selection?

From the above analysis results, we can know that as long as we get the dd element where the content we want to select is located and trigger a click event on it, we can realize the automatic selection operation when the select is loaded.

First you need to use lay-value to determine which element needs to be set for automatic selection

var select = &#39;dd[lay-value=&#39; + data.schoolId + &#39;]&#39;;
Copy after login

Trigger the click event to achieve automatic selection

$(&#39;#edit_exam_school&#39;).siblings("div.layui-form-select").find(&#39;dl&#39;).find(select).click();
Copy after login

The above is the detailed content of How to set select items in layui. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template