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

As$.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

親スコープで変数を宣言します。varstop_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 に答える