Home > Article > Web Front-end > How to set select items in layui
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.
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>
What is the rendering result of layui on the above html?
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:
#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 = 'dd[lay-value=' + data.schoolId + ']';
Trigger the click event to achieve automatic selection
$('#edit_exam_school').siblings("div.layui-form-select").find('dl').find(select).click();
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!