1

我遇到的问题是我无法访问函数外部变量的值。我刚刚开始学习 jQuery,我真的很喜欢它,但这已经让我坚持了很长一段时间。

我已经阅读了与此相关的其他线程,但这些方法仍然不起作用!

    $.get("/new/verify.php", { username: username, email: email },
        function(data){
            var stop_loading = '0';
            if(data == 'username')
            {
                alert('username taken');
                $('#username_taken_error').show();
                $('#username').focus();
                stop_loading = '1';
            }else if(data == 'email') {
                alert('email taken');
                $('#email_taken_error').show();
                $('#email').focus();
                stop_loading = '1';
            }else if(data == 'username|email') {
                alert('username/email taken');
                $('#username_taken_error').show();
                $('#email_taken_error').show();
                $('#username').focus();
                stop_loading = '1';     
            }
    });

    alert(stop_loading);

    if(stop_loading == '1') {
        alert('Stop loading');
        return false;   
    }
4

2 回答 2

2

由于$.get()执行异步 AJAX 请求,因此在您的$.get()成功函数中调用另一个函数,stop_loading如下所示:

 $.get("/new/verify.php", { username: username, email: email },
        function(data){
            var stop_loading = '0';
            if(data == 'username')
            {
                alert('username taken');
                $('#username_taken_error').show();
                $('#username').focus();
                stop_loading = '1';
            }else if(data == 'email') {
                alert('email taken');
                $('#email_taken_error').show();
                $('#email').focus();
                stop_loading = '1';
            }else if(data == 'username|email') {
                alert('username/email taken');
                $('#username_taken_error').show();
                $('#email_taken_error').show();
                $('#username').focus();
                stop_loading = '1';     
            }
            // call function with stop_loading
            callAFunction(stop_loading);
    });

    // this function will be called
    // with stop_loading arugment

    function callAFunction(stop_loading){
      if(stop_loading == '1') {
        alert('Stop loading');
        return false;   
      }
    }
于 2012-07-28T09:54:31.327 回答
1

在父范围内声明你的变量: var stop_loading = '0';

$.get("/new/verify.php", { username: username, email: email },
    function(data){

        if(data == 'username')
        {
            alert('username taken');
            $('#username_taken_error').show();
            $('#username').focus();
            stop_loading = '1';
        }else if(data == 'email') {
            alert('email taken');
            $('#email_taken_error').show();
            $('#email').focus();
            stop_loading = '1';
        }else if(data == 'username|email') {
            alert('username/email taken');
            $('#username_taken_error').show();
            $('#email_taken_error').show();
            $('#username').focus();
            stop_loading = '1';     
        }
});

alert(stop_loading);

if(stop_loading == '1') {
    alert('Stop loading');
    return false;   
}
于 2012-07-28T08:30:13.827 回答