How to retrieve input data in layui?
There are several ways to retrieve input data in layui.
- To retrieve using jQuery: Add an id attribute to the input element, then use a jQuery selector to get its value.
Example code:
var inputValue = $('#inputId').val();
- To retrieve using native JavaScript: add an id attribute to the input element, then use the document.getElementById() method to get the input element and fetch its value.
Example code:
var inputValue = document.getElementById('inputId').value;
- To obtain data using layui: By utilizing the form module of layui, listen for the form submission event with form.on(‘submit(formDemo)’, function(data){}), and then retrieve the form data with data.field.
Code example:
layui.use('form', function(){
var form = layui.form;
//监听表单提交
form.on('submit(formDemo)', function(data){
var inputValue = data.field.inputName;
//处理获取的数据
return false; //阻止表单跳转
});
});
In this case, ‘formDemo’ is the value of the lay-filter attribute of the form element, specifying the form to be monitored. ‘inputName’ is the value of the name attribute of the input element, specifying the input data to be retrieved.