Home > Web Front-end > JS Tutorial > body text

jquery serialized form and callback function usage examples_jquery

WBOY
Release: 2016-05-16 16:42:45
Original
1263 people have browsed it

In development projects, the front-end values ​​are passed to the back-end. Sometimes there are one or two values ​​in the JSP form, or all the values. If you pass them one by one at this time, it is definitely not a good idea, so use jQuery to provide The form serialization method can solve this problem very well. At the same time, it can be encapsulated into a general function. If the execution is successful, the respective callback function can be called to realize the respective functions.

The code is as follows:

function queryUserInfo(actionUrl,formId,fun){ 
var params=new Object(); //声明数组 
$.each($("#"+formId).serializeArray(),function(index,param){ 
params[param.name] = param.value; //序列化表单 
}); 
params['time']=new Date(); //1 
$.ajax( { 
url : basePath+actionUrl, 
data : params,//没有1,可以这样写("#"+formId).serializeArray() 
type : 'POST', 
dataType:'json', 
async: false,//表示同步,等待服务端返回数据,才会执行后面的代码 
success : function(obj) { 
fun(actionUrl,formId,obj); 
}, 
error: function() { 
alert("访问异常"); 
} 
}); 
}
Copy after login

Another way:

function setUserInfo(actionUrl,userid,username,fun){ 
var params=new Object(); //声明数组 
params['user.id']=userid; 
params['user.name']=username; 
$.ajax( { 
url : actionUrl, 
data : params,//没有1,可以这样写("#"+formId).serializeArray() 
type : 'POST', 
dataType:'json', 
async: false,//表示同步,等待服务端返回数据,才会执行后面的代码 
success : function(obj) { 
fun(actionUrl,formId,obj);//调用回调的函数 
}, 
error: function() { 
alert("访问异常"); 
} 
}); 
}
Copy after login
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template