从下面的编码中,如果某些数据有误,它会提示错误。如果确认的密码与密码不同,则可以正常工作。但是,如果 loginID 检查似乎有一些问题。它分别警告 true789 false false123 false456。为什么结果不是 false123 false456 false789 false?因为json的延迟?如何解决?
var result = "true";
$('#test').click(function(){
validation();
alert(result);
});
function validation(){
result = "true";
var loginID = $("#loginID").val();
var role = $("#role").text();
// check Login Name
if (loginID == "" || loginID == null){
$('#errorID').empty();
$('#errorID').append(
'<h6>' + "The Login Name cannot be empty" + '</h6>'
);
$("#errorID").show();
result="false";
} else {
$.ajax({
type: "GET",
dataType: "jsonp",
jsonpCallback: "jsoncallback",
data: {
ID: loginID,
role: role
},
url: "http://mydomain.com/checkID.php?jsoncallback=?",
success: function(data){
if (data[0].data.user_name!=""){
result="false";
alert(result+"123");
$('#errorID').empty();
$('#errorID').append(
'<h6>' + "The Login Name " + data[0].data.user_name +
" is used." + '</h6>'
);
$("#errorID").show();
alert(result+"456");
}
},
error: function(jqXHR, textStatus, errorThrown){
alert("Request failed: " + textStatus + errorThrown);
result="false";
}
}); //end of ajax
}
alert(result+"789");
return result;
} // end of #validation