How to verify the form submission method and serialize the Form Content in jQuery

Source: Internet
Author: User
The form submission method used in the previous project. The form () method can be used to separate the submission event from the submit button and bind it to any event, the following is a good example. You can refer to the form submission method used in the previous project.

The form () method can be used to separate the submitted event from the submit button and bind it to any event.

The Code is as follows:


Function addSubmit (){
$ ('# Addform'). form ('submit ',{
Url: _ basePath + '/@ Controller/@ RequestMapping ',
OnSubmit: function (){
If (boolean) {// conditions for determining whether to submit
$. Messager. show ({
Title: 'hup', msg: 'Do not meet the Save condition ',
ShowType: 'fade ', style: {right: '', bottom :''}
});
Return false; // block form submission
}
Return $ ('# addForm'). form ('validate'); // checks whether all required items have values.
},
Success: function (data ){
Var obj = jQuery. parseJSON (data); // convert the returned JSON to the desired object (ResponseData)
If (! Obj. success) {// determines the attribute value of the Status indicated in the returned ResponseData object
$. Messager. show ({
Title: 'hs', msg: 'failed to save ',
ShowType: 'fade ', style: {right: '', bottom :''}
});
} Else {
$. Messager. show ({
Title: 'hs', msg: 'saved successfully ',
ShowType: 'fade ', style: {right: '', bottom :''}
});
$ ("# AddWin"). window ("close"); // close the submission of pwkk
Query (); // refresh the result set
}
}
});
}


Today, I read this submission method in sharp jQuery, Using ajax to encapsulate the form content and using post to submit.

The Code is as follows:


$ ("# Btn"). click (function (){
$. Get ("get. php ", {username: $ (" # username "). val (), password: $ ("# password "). val ()}, function (data, textStatus) {// extract the data in the form one by one and then encapsulate and upload the data
$ ("# Target" ).html (data); // fill in the returned value to the page
});
});


Then there is a simplified version that uses the serialize () method for serialization.

The Code is as follows:


$ ("# Btn"). click (function (){
$. Get ("get. php", $ ("# form"). serialize (), function (data, textStatus) {// extract the data in the form one by one and encapsulate the upload
$ ("# Target" ).html (data); // fill in the returned value to the page
});
});


The serialize () method can be automatically encoded, and objects other than forms such as checkbox can also be converted using it.

In addition, the serializeArray () method can serialize elements and return JSON objects in array format instead of JSON strings.

You do not need to use the jQuery. parseJSON () method for conversion.

You can directly use methods like $. each () to return values.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.