jQuery(document).ready(function(jQuery) {
function checkEmail(email){
jQuery.post('someroute.php',
{email:eamil},
function(data){
if(data==error){
return false;
}
);
return true;
}
jQuery('#myform').submit(function(){
// como code and at the momment this
var result true;
//then I check email
var email = ('#myemail').val();
result = checkEmail(email);
return result;
});问题是,一个checkEmail函数,首先返回true,然后返回值jQuery.post函数。为什么?
我检查并在提交中,首先返回true,如果您停止提交,则释放post返回值。Post运行得很好,但我不明白为什么函数checkEmail不等到post返回值,而是一直到最后返回true。
发布于 2012-10-08 06:00:12
因为jQuery.post()函数是一个AJAX请求,而AJAX中的第一个A代表异步。这正是异步的本质,它不会在等待响应时停止代码执行。它触发请求,然后立即转移到发出请求的行之后的任何代码,在本例中是return true语句。
发布于 2012-10-08 06:51:50
就像安东尼·格里斯特说的。.post是一个异步调用,这意味着它不需要等待完成。
我查找了.post方法(http://api.jquery.com/jQuery.post/)。
这基本上是.ajax的快捷方式。这里没有使用fail方法,所以我建议使用.ajax调用。
var value = 1;
var handlerUrl = "someroute.php";
//Do the Ajax Call
jQuery.ajax(
{
url: handlerUrl,
data: { email:eamil },
type: 'POST',
success: function (data)
{
return true;
},
error: function (jxhr, msg, err)
{
return false;
}
});发布于 2012-10-08 06:58:54
@user1727336 :嗨!您可能需要编辑代码并添加缺少的}和});。这里有一个解决方案:
// enter code here
jQuery(document).ready(function(jQuery) {
function checkEmail(email){
jQuery.post('someroute.php', {email:eamil},
function(data){
if(data==error){
return false;
}
});
return true;
}
});https://stackoverflow.com/questions/12773209
复制相似问题