如何使返回值为true和false。
我总是未定义,在阅读了一些文章之后,我仍然不理解。
我的参考:How to return value from an asynchronous callback function?
function validateScreenName(value){
var screenname = document.getElementById("screenname-box").value;
if((screenname.length < 3) || (screenname.length > 20)){
value(false); // if I change to return false still can't get the value. Always undefined.
}else{
if(screenname != "something"){
var http = new XMLHttpRequest();
var param = "screen_name="+screenname;
var url = "screen-name-validation.php";
http.open("POST",url,true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function(){
//The response from server is either 1 or 0
if (http.readyState==4 && http.status==200){
if(http.responseText == 0){
value(true); // I can't get this value
}else{
value(false);
}
}
}
http.send(param);
}else{
value(false);
}
}
}
我需要这个脚本的值
function disableSubmit(){
validateScreenName(function(val2){
var val1 = validateFullName();
var val3 = validateTargetName();
//I need val2 from validateScreenName
if(val1 && val2 && val3){
//if all true do something
}else{
//if one of the value false do somthing
}
});
}
有人可以解释如何在没有jquery的情况下仅使用javascript获取值吗?感谢。
答案 0 :(得分:-1)
不使用返回typeof bool(false,true),而是使用返回string..eg。,“true”或“false”