How can I retrieve form data in layui?

There are two commonly used methods to retrieve form data in layui.

  1. To obtain form data, one must first include the Form module from layui and then listen for the submit event.
  2. The following code uses the ‘form’ module from layui:
    “`
    layui.use(‘form’, function(){
    var form = layui.form;
    });
    “`
  3. Next, listen to the submit event of the form to get the form data, using form.on(‘submit(formDemo)’, function(data){}), where formDemo is the value of the form’s lay-filter attribute.
  4. When the form with the ID “formDemo” is submitted, the function will be triggered with the form data. This includes all the fields and their corresponding values. The “return false” statement prevents the form from redirecting.
  5. In the code above, data.field represents the form data, and you can retrieve the value of a specific field by using data.field.fieldName.
  6. To retrieve form data, you can use either jQuery or native JavaScript. First, you need to render the form using layui’s layui.form.render() method, and then you can use jQuery’s serialize() method to get the form data.
  7. The following code utilizes the form and jQuery modules from the layui library. It renders the form and prevents the default form submission. It then serializes the form data when the form with the ID “formId” is submitted.
  8. In the above code, formId is the id attribute value of the form, and you can retrieve form data using $(this).serialize().
bannerAds