为什么usernameOK = 1;当我在$ .post函数后使用alert语句时,ajax函数中的语句不会保持设置?我需要一种方法来使用变量作为if语句的条件。
function validateRegistration() {
var username = document.getElementById("userName").value;
var fname = document.getElementById("fName").value;
var lname = document.getElementById("lName").value;
var email = document.getElementById("email").value;
var confirmEmail = document.getElementById("confirmEmail").value;
var password = document.getElementById("userpass").value;
var confirmPass = document.getElementById("confirmPassword").value;
var usernameOK = 0;
$.post("action.php", { field: 'doesUsernameExist', username: username },
function(result){ //if the result is 1
if(result == 1){ //show that the username is available
usernameSuc.innerHTML= username + " is Available.";
var usernameOK=1;
}else if(result == 0){ //show that the username is NOT available
usernameErr.innerHTML="User name: "+username+" is not
available.";
} else {
usernameErr.innerHTML="Something is Wrong";
}
});
alert("usernameOK: "+usernameOK); //This doesn't work. only prints a zero, never 1
if (usernameOK == 1) {
if ((username.length > 5) && (!(/[^a-zA-Z0-9_-]/.test(username)))){
if((fname != "") && (lname != "")){
if((email != "") && (email == confirmEmail)){
if (((email.indexOf(".") > 0) && (email.indexOf("@") > 0))
&& !/[^a-zA-Z0-9.@_-]/.test(email)){
if ((password.length > 7) && (/[a-z]/.test(password))
&& (/[A-Z]/.test(password)) && (/[0-9]/.test(password))){
if (password == confirmPass) {
document.getElementById("submit").className = "btn btn-success";
document.getElementById("submit").removeAttribute("disabled");
}
}
}
}
}
}
} else {
document.getElementById("submit").className = "btn btn-success disabled";
}
}
答案 0 :(得分:0)
找出解决方案。以为我会发布它,以防它可以帮助别人。更改了顺序并在所有条件if语句中移动了ajax,因此它是最后检查而不是第一个。
function validateRegistration() {
var username = document.getElementById("userName").value;
var fname = document.getElementById("fName").value;
var lname = document.getElementById("lName").value;
var email = document.getElementById("email").value;
var confirmEmail = document.getElementById("confirmEmail").value;
var password = document.getElementById("userpass").value;
var confirmPass = document.getElementById("confirmPassword").value;
if ((username.length > 5) && (!(/[^a-zA-Z0-9_-]/.test(username)))){
if((fname != "") && (lname != "")){
if((email != "") && (email == confirmEmail)){
if (((email.indexOf(".") > 0)
&& (email.indexOf("@") > 0))
&& !/[^a-zA-Z0-9.@_-]/.test(email)){
if ((password.length > 7)
&& (/[a-z]/.test(password))
&& (/[A-Z]/.test(password))
&& (/[0-9]/.test(password))){
if (password == confirmPass) {
$.ajax({
type: "POST",
url: "action.php",
data: {field: 'doesUsernameExist', username: username },
async: false,
success: function (result) {
if(result == 1) {
document.getElementById("submit").className = "btn btn-success";
document.getElementById("submit").removeAttribute("disabled");
} else {
document.getElementById("submit").className = "btn btn-success disabled";
document.getElementById("submit").setAttribute('disabled', true);
}
},
error: function (result) {
pass = "error"
alert("There was an error with the database");
}
});
}
}
}
}
}
} else {
document.getElementById("submit").className = "btn btn-success disabled";
document.getElementById("submit").setAttribute('disabled', true);
}
}