How to get the value of a selected option in layui?
You can use the form module in Layui to get the value of the selected option in a dropdown menu.
Firstly, make sure to import the Layui form module into the page, as shown below:
<script src="layui/layui.js"></script>
Next, add a ‘lay-filter’ attribute to the place where the select element is used to identify the select element, as shown below:
<select name="select" lay-filter="select">
<option value="">请选择</option>
<option value="1">选项1</option>
<option value="2">选项2</option>
<option value="3">选项3</option>
</select>
Next, you will write the corresponding JavaScript code on the page, using the on() method of the form module to listen for the change event of the select element, and then retrieve the selected value in the event handling function as shown below:
layui.use('form', function() {
var form = layui.form;
form.on('select(select)', function(data) {
console.log(data.value); // 获取选中的值
});
});
When a user chooses an option in the select menu, it triggers the change event, and then the selected value can be accessed through data.value.
The above is the method of using Layui to retrieve the selected value from a select element.