我想提交注册表单。在 onload 表单中,我调用了一个 JavaScript 函数来验证所有字段。
这里我应该调用一个ajax函数来验证一个字段。
var checkExists = true;
checkExists = ajaxQuery(data);
if (checkExists) {
alert('Already exists');
return false;
}
ajaxquery
是一个 ajax 函数,它将根据结果返回 true 或 false。
我的问题是,无论AJAX结果是什么,它都会首先执行if
条件并返回false。
因此,即使 checkExists
变量值为 false
,我的表单也总是卡住。请帮忙
最佳答案
因为ajax查询是异步的,所以需要将代码放在ajax查询的callback方法中:http://api.jquery.com/jQuery.ajax/
$.ajax({
url: "test.html",
context: document.body,
success: function(data){
// your logic goes here.
alert('Already exists');
return false;
}
});
关于jquery - 完成 Ajax 请求后提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5485687/