How to get form data in layui

下次还敢
Release: 2024-04-04 03:39:21
Original
939 people have browsed it

layui provides a variety of methods for obtaining form data, including directly obtaining all field data of the form, obtaining the value of a single form element, using the formAPI.getVal() method to obtain the specified field value, serializing the form data and AJAX request parameters, and listening to form submission events to obtain data.

How to get form data in layui

How layui gets form data

layui provides several methods to get form data:

1. layui.form.val(form selector)

This is a convenient method to directly obtain all field data of the entire form.

<code class="javascript">layui.form.val('form selector', {
  name: 'value',
  ...
});</code>
Copy after login

2. lay() method

The lay() method can get the value of a single form element and needs to pass the element's ID or DOM object.

<code class="javascript">const value = $('#input-id').val();</code>
Copy after login

3. formAPI.getVal() method

The getVal() method provided by formAPI can get the value of the specified field in the form.

<code class="javascript">const formAPI = layui.form.render('form selector');
const value = formAPI.val('field-name');</code>
Copy after login

4. layui.request() method

layui.request() method can serialize form data into a string and serve as parameters for AJAX requests.

<code class="javascript">layui.request.post('/submit_form', {
  data: $('#form').serialize()
});</code>
Copy after login

5. layui.form.on('submit()', callback)

Listen to the form submission event and obtain the form data in the callback function.

<code class="javascript">layui.form.on('submit(submit-btn)', function(data) {
  console.log(data.field); // 表单字段数据
});</code>
Copy after login

According to your needs, you can choose one or more of the above methods to obtain layui form data.

The above is the detailed content of How to get form data 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!