代码之家  ›  专栏  ›  技术社区  ›  user350034

jquery get函数返回真/假

  •  5
  • user350034  · 技术社区  · 16 年前
    $(document).ready(function(){
     //global vars
     var name = $("#username");
        var email = $("#email");
    
    
     function usernameExists() {
      $.get("register.php",{ check: 1, username: name.val(), email: email.val() } ,function(m) {
          if(m==1) {
         return false;
        } else { 
         return true;
        }
      });
     }
    });
    

    Firebug在调用此函数时显示正确的响应,但它什么也不返回…(此$.get(…)函数已在函数usernameexists()之外进行了测试,但没有返回,并且工作正常)。

    问题是什么?如何解决?


         $(document).ready(function(){
        //global vars
        var form = $("#register");
        var name = $("#username");
        var email = $("#email");
    
         $.get("register.php",
                 { check: 1, username: name.val(), email: email.val() },
    
                   // Have this callback take care of the rest of the submit()
                 function(m) {
                    if(m==1) {
                         form.submit(function(){ return false; });
                    } else {
                        form.submit(function(){
            if(validateName() & validateEmail() & validatePass1() & validatePass2())
                return true
            else
                return false;
                    });
                 }
    
             }
          );
    
    function validateName(){
            // some check here
        }
    
    // and other functions
    
    });
    
    1 回复  |  直到 14 年前
        1
  •  8
  •   user113716    16 年前

    您调用的函数不返回任何内容。

    即使它试图从您的 $.get() 因为调用是异步的,所以在收到响应时,无论使用什么代码,返回值都可能已经执行。

    您需要做的是从 $GET() 回调。

    function usernameExists() {
        $.get("register.php",{ check: 1, username: name.val(), email: email.val() } ,function(m) {
                someOtherFunction(m==1);
        });
    }
    
    function someOtherFunction(parameter) {
        // The parameter will be true or false
        //    depending on the value of m==1
    }
    

    根据您的评论更新。

    最好是把 $GET() 进入 submit() 但要忠于你最初的想法,这就是它看起来的样子。

    form.submit(function(){
           // After usernameExists() is called, we need to hand off
           //    the rest of the execution to that function since
           //    this one will be done executing before the get()
           //    response is received
        usernameExists();
        return false;
    }); 
    
    function usernameExists() {
        $.get("register.php",
                 { check: 1, username: name.val(), email: email.val() },
    
                   // Have this callback take care of the rest of the submit()
                 function(m) {
                    if(m==1) {
                         // do something if true
                    } else {
                         // do something if false
                    }
                 }
          );
    }
    

    解释同步与异步javascript的乐趣

    JavaScript代码通常同步执行。这意味着它一次执行一行,或者一行必须在下一行启动之前完成执行。

    var greeting = "hi there";  // set the greeting variable
    
    alert( greeting );   // the alert won't fire,
                         //    until the previous line finished successfully
    

    这使得事情变得非常美好和可预测。但也有一些例外。一个显著的例外是Ajax调用。

    你的 $GET() 是Ajax调用的一个示例。Ajax中的“A”代表异步,这意味着它不会阻止下一行代码的执行。

    其后果是当你做一个 $GET() 这需要(例如)1秒钟才能完成,无论在 $GET() 早就完成了 $GET() 已收到响应。

    取上一个 greeting 例如,但这次使用的是Ajax。

    var greeting;  // will hold the response from our AJAX call
    
    $.get('some/path/to/data.php', 
             function( m ) {
                 greeting = m;  // populate the greeting variable with the data returned
             }
    );
    
    alert( greeting );   // Will alert "undefined" instead of the data that was returned
                         //   because the $.get() code above is asynchronous, which allows
                         //   the code below it (the alert in this case) to continue 
                         //   executing.
    

    如你所见, alert( greeting ) 早在 $GET() 因为他 $GET() 是异步的,并且在等待数据时不会暂停执行链。

    为了解决这个问题,您可以将 alert() 里面 的回调 $GET() ,以便在收到响应之前不会运行。

    var greeting;  // will hold the response from our AJAX call
    
    $.get('some/path/to/data.php', 
             function( m ) {
                 greeting = m;  // populate the greeting variable with the data returned
                 alert( greeting );  // Now the alert will give the expected result
                                     //    because it is in the callback.
             }
    );
    

    结果就是在你的代码中,一旦你打电话 $GET() ,任何依赖于收到的响应的剩余代码都应该发生 里面 回调。

    放置代码的唯一方法 外部 回调将把它放在自己的函数中,该函数从 里面 回拨(就像我最初的回答一样)。


    代码应如何操作的基本布局:

    请记住,您不必为 usernameExists() . 你可以把所有的代码放在 提交()

    form.submit(function() {
           // Check to make sure input is valid **before** you send the AJAX
        if(validateName() & validateEmail() & validatePass1() & validatePass2()) {
            usernameExists();  // If valid, continue with the usernameExists()
        }
        return false; // We return false whether or not the content was valid,
                      //   in order to prevent the form from submitting prematurely
    }); 
    
    function usernameExists() {
        $.get("register.php",
                 { check: 1, username: name.val(), email: email.val() },
    
                   // Have this callback take care of the rest of the submit()
                 function(m) {
                       // If "m" is less than one, there were no existing users
                       //    so we can go ahead and post the data to the server
                    if( parseInt(m) < 1 ) {
                         // Here, you would need to manually do a post to 
                         //   submit the data to the server
                         $.post(url, data, callback, datatype );
                    }
                 }
         );
    }
    

    http://api.jquery.com/jquery.post/