写了一个函数返回值是 true 或者 false
但是在调用的时候却返回 undefined如例: function chk() { return false }在另一个例子调用时:var ba=chk();return ba 却返回 undefined为什么????

解决方案 »

  1.   


    <script>
    function chk() { return false } 
    function test(){var ba=chk();return ba}alert(test());
    </script>没出现undefined,LZ怎么调用的?
      

  2.   

    function AjaxCheckAccount() {
      xmlHttp = GetXmlHttpObject(); var url = "ActionUser.asp?a=chkuser&s="+escape($("Account").value); xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState == 1){ $("strAccount").innerHTML="请梢后,正在发送中..."; $("strAccount").className="Reg"; } if (xmlHttp.readyState == 4){ var result = xmlHttp.responseText; var json = eval("(" + result + ")"); $("strAccount").innerHTML=json.innerHTML; $("strAccount").className=json.className; var reUrl=(json.reValue=="True"?true:false);return reUrl;}}; xmlHttp.open("GET",url,true); xmlHttp.send(null);  
    }调用function blurchk(objid) {
      var valueBack = true; var valueObjid = $(objid).value;
      if(objid=="Account") {if(valueObjid=="") {$("str"+objid).innerHTML = "请输入您常用的Email,即用来登录您的帐户。";$("str"+objid).className = "Error";valueBack = false; } else { if(checkEmail("Account","strAccount","帐户输入格式不正确,为Email格式!")==true) { valueBack=AjaxCheckAccount();alert(valueBack); }}} }
      

  3.   

    贴出你完整的代码瞧瞧
    return ba也在一个函数里?
      

  4.   

    你对onreadystatechange的理解是错误的
      

  5.   

    好像是应该返回undefined,你在AjaxCheckAccount最后加上return 'test'看看
    xmlHttp.send(null);
    return "test";
      

  6.   

    AJAX is asynchronous, meaning it takes place while other AjaxCheckAccount is going on. Thus, the return value of the AjaxCheckAccount function will always be irrelevant, as the code that called it won't wait for the AJAX request to finish before checking for the return value - so it will always be undefined.Instead, you have to use callbacks. This means that the function that is making the AJAX request does so, then stops execution. When the AJAX request completes, it calls another function - it can be a new one, or it can re-call the original with a different callback value, and execution can continue where you want it, complete with the data you need.
      

  7.   


    还是返回 undefined 啊~~
      

  8.   

    onreadystatechange 在函数内部并没有执行吧
      

  9.   


    function AjaxCheckAccount() {
      var reUrl;
      xmlHttp = GetXmlHttpObject(); var url = "ActionUser.asp?a=chkuser&s="+escape($("Account").value); xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState == 1){ $("strAccount").innerHTML="请梢后,正在发送中..."; $("strAccount").className="Reg"; } if (xmlHttp.readyState == 4){ var result = xmlHttp.responseText; var json = eval("(" + result + ")"); $("strAccount").innerHTML=json.innerHTML; $("strAccount").className=json.className; reUrl=(json.reValue=="True"?true:false);return reUrl;}}; xmlHttp.open("GET",url,true); xmlHttp.send(null); alert(reUrl);
    }我在 reUrl=(json.reValue=="True"?true:false); 代码后加入 alert(reUrl) 就会返回 true 或 false
    但在 xmlHttp.send(null); 后面就返回 undefined 
      

  10.   

    看不懂7楼的英文!onreadystatechange指向的是一个事件侦听函数!要想返回值你只能把它指向另一个函数
    类4:
    onreadystatechange=callbackfun;
      

  11.   

    再不明白看这:
    http://stackoverflow.com/questions/562412/return-value-from-function-with-an-ajax-call
      

  12.   

    异步!
    解决方法:把所有用ajax从服务端取回的数据后的操作放进callback回调函数中操作。
      

  13.   


    我看了,可以说按他那个来的,可是还是不行啊~~function AjaxCheckAccount() {
      var reAccount=false;
      xmlHttp = GetXmlHttpObject();
      var url = "ActionUser.asp?a=chkuser&s="+escape($("Account").value);
      xmlHttp.open("GET",url,true);
      xmlHttp.send(null);
      
      xmlHttp.onreadystatechange = function() {
        if(xmlHttp.readyState == 1) {
      $("strAccount").innerHTML="请梢后,正在发送中...";
      $("strAccount").className="Reg";
    }
        if (xmlHttp.readyState == 4){
          var result = xmlHttp.responseText;
          var json = eval("(" + result + ")");
          $("strAccount").innerHTML=json.innerHTML;
          $("strAccount").className=json.className;
          reAccount=(json.reValue=="True"?true:false);
    }
      }
      alert(reAccount);
      return reAccount;
    }如果我不给变量初始值,还是去返回undefined 
      

  14.   

    把onreadystatechange调用的函数改成回调的试试
      

  15.   

    function AjaxCheckAccount() {
      var reAccount=false;
      xmlHttp = GetXmlHttpObject();
      var url = "ActionUser.asp?a=chkuser&s="+escape($("Account").value);
      xmlHttp.open("GET",url,true);
      xmlHttp.send(null);
      
      xmlHttp.onreadystatechange = function() {
        if(xmlHttp.readyState == 1) {
          $("strAccount").innerHTML="请梢后,正在发送中...";
          $("strAccount").className="Reg";
        }
        if (xmlHttp.readyState == 4){
          var result = xmlHttp.responseText;
          var json = eval("(" + result + ")");
          $("strAccount").innerHTML=json.innerHTML;
          $("strAccount").className=json.className;
          reAccount=(json.reValue=="True"?true:false);
        }
      }
      alert(reAccount);
      return reAccount;
    }
    这是有问题的。
    ajax是异步执行的。也就是说它不是按你的顺序从上往下执行。当ajax发送请求时,服务器端要处理,要时间,这时候,js继续往下执行,reAccount根本就还没有值。