Home >Web Front-end >JS Tutorial >Using ajax to submit form forms based on jQuery through jQuery.form.js plug-in_javascript skills
When we submit the form, if we do not use ajax to submit, the page will refresh itself, which is very unfriendly, so we need to modify our form submission to ajax mode to let users clearly know that they are in At which stage is the form submitted: Submitting? Submission successful?
I used it briefly. The jQuery Form plug-in has the following advantages:
1. Support pre-submission verification.
2. Support callback after submission.
3. Adopt AJAX method and have a good user experience
4. The submission method is flexible. Just specify the form ID to be submitted. You can submit that form if you want to submit it. The submission parameters are configurable at the same time.
5. Supports submission of multiple types of data. Such as: xml, json, etc.
Main functions:
1.ajaxForm
Add all required event listeners to prepare the form for AJAX submission. ajaxForm cannot submit the form. In the document's ready function, use ajaxForm to prepare for AJAX submission of the form. ajaxForm accepts 0 or 1 parameters. This single parameter can be either a callback function or an Options object.
Example:
$('#myFormId').ajaxForm();
2.ajaxSubmit
The form will be submitted immediately via AJAX. In most cases, ajaxSubmit is called to respond to the user submitting the form. ajaxSubmit accepts 0 or 1 parameters. This single parameter can be either a callback function or an Options object.
Example:
// 绑定表单提交事件处理器 $('#myFormId').submit(function() { // 提交表单 $(this).ajaxSubmit(); // 为了防止普通浏览器进行表单提交和产生页面导航(<span style="color: #333333;"><a href="http://www.100sucai.com/web/jquerytexiao/huandengpian" target="_blank"><span style="color: #333333;">防止页面刷新</span></a></span>?)返回false return false; });
3.formSerialize
Serialize (or serialize) the form into a query string. This method will return a string in the following format: name1=value1&name2=value2. This method returns a string.
Example:
var queryString = $('#myFormId').formSerialize(); // 现在可以使用$.get、$.post、$.ajax等来提交数据 $.post('myscript.php', queryString);
4.fieldSerialize
Serialize (or serialize) the form's field elements into a query string. This is convenient when only some form fields need to be serialized (or serialized). This method will return a string in the following format: name1=value1&name2=value2. This method returns a string.
Example:
var queryString = $('#myFormId .specialFields').fieldSerialize();
5.fieldValue
Returns the form element value that matches the inserted array. As of version 0.91, this method will always return data as an array. If the element value is judged to be potentially invalid, the array is empty, otherwise it contains one or more element values. This method returns an array.
Example:
// 取得密码输入值 var value = $('#myFormId :password').fieldValue(); alert('The password is: ' + value[0]);
6.resetForm
Restore the form to its initial state by calling the original DOM method of the form element.
Example:
$('#myFormId').resetForm();
7.clearForm
Clear form elements. This method clears all text input fields, password input fields, and textarea fields, clears the selection in any select elements, and clears all radio buttons and multi-selects. (checkbox) button resets to its unselected state.
Example:
$('#myFormId').clearForm();
8.clearFields
Clear field elements. It is convenient to use only when some form elements need to be cleared.
Example:
$('#myFormId .specialFields').clearFields();
Simple example of jQuery Form plugin:
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My Jquery</title> <script type="text/javascript" src="<%=request.getContextPath()%>/jslib/jquery-1.4.2.js"></script> <script type="text/javascript" src="<%=request.getContextPath()%>/jslib/jquery.form.js"></script> <script type="text/javascript"> // wait for the DOM to be loaded $(document).ready(function() { // bind 'myForm' and provide a simple callback function $('#myForm').ajaxForm(function() { alert("Thank you for your comment!"); }); }); // attach handler to form's submit event $('#myFormId').submit(function() { // submit the form $(this).ajaxSubmit(); // return false to prevent normal browser submit and page navigation return false; }); </script> </head> <body> <form id="myForm" action="index.jsp" method="post"> Name: <input type="text" name="name" /> Comment:<textarea name="comment"></textarea> <input type="submit" value="Submit Comment" /> </form> </body> </html>
This article introduces in detail how jQuery uses ajax to submit form forms through the jQuery.form.js plug-in. I hope you like it.