2015-08-23 34 views
1

我有一個HTML表單:jQuery的崗位不是每次沃金的Firefox

<form method="post" id="input_form" onSubmit="return save_mail();"> 
    <input type="text" value="enter your email" id="mail" name="mail"/> 
    <button type="submit" id="save_btn" >Sign up</button> 
    <div id="warning"></div> 
</form> 

的JavaScript代碼如下所示:

function validateEmail(email) { 
    var re = /\[email protected]\S+\.\S+/; 
    return re.test(email); 
} 

function wrongEmail() { 
    document.getElementById("warning").innerHTML="wrong email format"; 
} 

function rightEmail() { 
    document.getElementById("warning").innerHTML=""; 
} 

function save_mail() { 
    var pom=document.getElementById("mail").value; 
    if (validateEmail(pom)) { 
     $.post("php/sign_up.php", {mail : pom}); 
     rightEmail(); 
     alert("heey"); // if I remove this alert, it doesn't work on FF anymore 
     return true; 
    } 
    else { 
     wrongEmail(); 
     return false; 
    } 
} 

這一切工作正常,在Chrome也火狐,但如果我刪除來自我的JS代碼的警報,它不再工作。我不知道,這怎麼可能。根據FF或Chrome沒有錯誤。

回答

0

$.post默認情況下是異步的,所以後面的所有代碼都會在ajax請求完成後直接執行,而不是完成之後,因爲我認爲您可能期待。

爲了讓Ajax請求後的代碼執行完成化妝用的延遲功能或成功回調$.ajax

$.post("php/sign_up.php",{mail: pom}, function(){ 
    //this is where you execute code once the sign_up.php script has been called 
}) 
0

添加你內心的jQuery後的成功行動,jQuery的推薦款式是

var jqxhr = $.post("example.php", function() { 
    alert("success"); 
}) 
.done(function() { 
    alert("second success"); 
}) 
.fail(function() { 
    alert("error"); 
}) 
.always(function() { 
    alert("finished"); 
});