Home Web Front-end JS Tutorial Jquery submission form Form.js official plug-in introduction_jquery

Jquery submission form Form.js official plug-in introduction_jquery

May 16, 2016 pm 05:55 PM

Let’s first talk about the commonly used Form plug-ins, which support Ajax and Ajax file upload. They are powerful and can basically meet daily applications.
1. Download the latest JQuery framework software package
jquery.js compressed package
jquery.js non-compressed package
2. Download the Form plug-in
form.js

3. A simple introduction to the Form plug-in
Step 1: Add a form
Code:

Copy the code The code is as follows:


Name:
Comment:



Step 2: Include the jquery.js and form.js files
code:
Copy code The code is as follows:






3. Detailed usage and application examples of the Form plug-in
http://www.malsup.com/jquery/form/
=========== =================
The author of this plug-in said this when introducing form.js:
Quote:
Submitting a form with AJAX doesn't get any easier than this!
It means that you can't get any easier than this when using Ajax to submit a form. Haha——, whether it blows water or not, you will know after using it.

Form plug-in API

English original text: http://www.malsup.com/jquery/form/#api
The form plug-in API provides several methods to allow you to easily manage Form data and making form submissions.
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.
Chainable: Yes.
Example:
Code:
Copy code The code is as follows:

$( '#myFormId').ajaxForm();
ajaxSubmit

The form will be submitted by AJAX immediately. 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.
Chainable: Yes.
Example:
Code:
Copy code The code is as follows:

// Bind the form submission event handler
$('#myFormId').submit(function() {
// Submit the form
$(this).ajaxSubmit();
// In order to prevent Ordinary browsers perform form submission and generate page navigation (prevent page refresh?) return false
return false;
});
formSerialize

Serialize the form (or serialized) into a query string. This method will return a string in the following format: name1=value1&name2=value2.
Chainable: No, this method returns a string.
Example:
Code:
Copy code The code is as follows:

var queryString = $('#myFormId').formSerialize();
// Now you can use $.get, $.post, $.ajax, etc. to submit data
$.post('myscript.php', queryString );
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.
Chainable: No, this method returns a string.
Example:
Code:
var queryString = $('#myFormId .specialFields').fieldSerialize();
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.
Chainable: No, this method returns an array.
Example:
Code:
Copy code The code is as follows:

// Get the password input value
var value = $('#myFormId :password').fieldValue();
alert('The password is: ' value[0]);
resetForm

Restore the form to its initial state by calling the original DOM method of the form element.
Chainable: Yes.
Example:
Code:
$('#myFormId').resetForm();
clearForm
Clear the form element. 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.
Chainable: Yes.
Code:
$('#myFormId').clearForm();
clearFields
Clear field elements. It is convenient to use only when some form elements need to be cleared.
Chainable: Yes.
Code:
$('#myFormId .specialFields').clearFields();
Options object
Both ajaxForm and ajaxSubmit support numerous option parameters, which can be provided using an Options object . Options is just a JavaScript object, which contains the following collection of attributes and values:
target
indicates the element in the page that is updated by the server response. The element's value may be specified as a jQuery selector string, a jQuery object, or a DOM element.
Default value: null.
url
Specifies the URL for submitting form data.
Default value: the action attribute value of the form
type
Specifies the method for submitting form data: "GET" or "POST".
Default value: The method attribute value of the form (defaults to "GET" if not found).
beforeSubmit
Callback function called before the form is submitted. The "beforeSubmit" callback function is provided as a hook to run pre-submit logic or validate form data. If the "beforeSubmit" callback function returns false, the form will not be submitted. The "beforeSubmit" callback function takes three calling parameters: form data in the form of an array, jQuery form object, and the Options object passed in ajaxForm/ajaxSubmit. The form array accepts data in the following manner:
Code:
[ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ]
Default value :null
success
Callback function called after the form is successfully submitted. If a "success" callback function is provided, it is called when the response is returned from the server. Then the dataType option value determines whether the value of responseText or responseXML is returned.
Default value: null
dataType
The data type expected to be returned. One of null, "xml", "script" or "json". dataType provides a method that specifies how to handle the server's response. This is reflected directly into the jQuery.httpData method. The following values ​​are supported:
'xml': If dataType == 'xml', the server response will be treated as XML. At the same time, if the "success" callback method is specified, the responseXML value will be returned.
'json': If dataType == 'json', the server response will be evaluated and passed to the "success" callback method, if it is specified.
'script': If dataType == 'script', the server response will evaluate to plain text.
(Annotation: Some of the places above were not clear, so I had to paraphrase them, hoping to express the original meaning.)
Default value: null (the server returns the responseText value)
semantic
Boolean flag indicating whether data must be submitted in strict semantic order (slower). Note that the normal form serialization is done in semantic order with the exception of input elements of type="image". You should only set the semantic option to true if your server has strict semantic requirements and your form contains an input element of type="image".
Boolean flag indicating whether the data must be submitted in strict semantic order (slower?). Note: Generally speaking, forms are serialized (or serialized) in semantic order, except for input elements of type="image". If your server has strict semantic requirements and the form contains an input element of type="image", you should set semantic to true. (Translation note: Because this paragraph is incomprehensible, the translation may not be clear, but please correct me.)
Default value: false
resetForm
Boolean flag, indicating whether to reset if the form is submitted successfully.
Default value: null
clearForm
Boolean flag, indicating whether to clear the form data if the form is submitted successfully.
Default value: null
Instance:
Code:
[/code]
// Prepare Options object
var options = {
target: '#divToUpdate',
url: 'comment.php',
success: function() {
alert('Thanks for your comment!');
} };
// Pass options to ajaxForm
$('#myForm').ajaxForm(options);
[/code]
Note: The Options object can also be used to pass values ​​to jQuery’s $.ajax method. If you are familiar with the options supported by $.ajax, you can use them to pass Options objects to ajaxForm and ajaxSubmit.
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

How do I optimize JavaScript code for performance in the browser? How do I optimize JavaScript code for performance in the browser? Mar 18, 2025 pm 03:14 PM

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

How do I debug JavaScript code effectively using browser developer tools? How do I debug JavaScript code effectively using browser developer tools? Mar 18, 2025 pm 03:16 PM

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

How do I use source maps to debug minified JavaScript code? How do I use source maps to debug minified JavaScript code? Mar 18, 2025 pm 03:17 PM

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

Getting Started With Chart.js: Pie, Doughnut, and Bubble Charts Getting Started With Chart.js: Pie, Doughnut, and Bubble Charts Mar 15, 2025 am 09:19 AM

This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3). Create pie and ring charts Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election. Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means any entity with zero proportion

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

TypeScript for Beginners, Part 2: Basic Data Types TypeScript for Beginners, Part 2: Basic Data Types Mar 19, 2025 am 09:10 AM

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript

See all articles