我正在使用jQuery表单插件向服务器提交数据。在提交之前,我正通过ajax运行服务器端验证。所以结构是从jquery ajax响应中获取值而不设置异步false
function validateForm(formData, jqForm, options){
var check = true; //have to set check to false to avoid form submit
....
looping through form elements and putting values in to data array here
....
function sendData(callback){
$.ajax({
url:'validate.php',
data:data,
dataType:'json',
//async:false, if I uncomment this, code works as I want
success:callback
});
}
function processForm(response){
$.each(response,function(i,res){
//if validation is fail I'm setting check = false here
});
}
sendData(processForm);
return check;
})
因为我看到设置异步为false是不好的做法,我怎么能设置检查值为false使用回调?
你能给我举个例子吗? – ThilinaG 2011-05-15 07:54:58
替换“/ /如果验证失败我在这里设置检查=假”与“做任何你想做的事情,如果检查是假的” – Quentin 2011-05-15 09:08:53
如果验证失败,我需要从此函数返回false,所以jquery表单插件块提交。 – ThilinaG 2011-05-15 14:39:27