Examples of jquery serialized forms and callback Functions

Source: Internet
Author: User

In a development project, the foreground value is passed to the background. Sometimes there are two or more values in the JSP form, and all values are included. If one value is uploaded one by one, it is definitely not a good solution, therefore, using the form serialization method provided by jQuery can solve this problem well. It can also be encapsulated into common functions. After successful execution, you can call their respective callback functions to implement their respective functions.

The Code is as follows:

Function queryUserInfo (actionUrl, formId, fun) {var params = new Object (); // declare an array $. each ($ ("#" + formId ). serializeArray (), function (index, param) {params [param. name] = param. value; // serialized form}); params ['time'] = new Date (); // 1 $. ajax ({url: basePath + actionUrl, data: params, // No 1, you can write it like this ("#" + formId ). serializeArray () type: 'post', ype: 'json', async: false, // indicates synchronization. The following code success will be executed only after the server returns data: function (obj) {fun (actionUrl, formId, obj) ;}, error: function () {alert ("Access exception ");}});}

Another method:

Function setUserInfo (actionUrl, userid, username, fun) {var params = new Object (); // declare the array params ['user. id'] = userid; params ['user. name'] = username; $. ajax ({url: actionUrl, data: params, // No 1, you can write it like this ("#" + formId ). serializeArray () type: 'post', ype: 'json', async: false, // indicates synchronization. The following code success will be executed only after the server returns data: function (obj) {fun (actionUrl, formId, obj); // call the callback function}, error: function () {alert ("Access exception ");}});}

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.