1

登録フォームがあり、jquery で 3 つの関数を作成しました。最初の 1 つはフォームの検証です。2 つ目は、ajax リクエストでメールの一意性を確認するためのものです。3つ目は、これもajaxリクエストでユーザーを作成するためのものです。

送信イベントでの私のフローは、最初に検証関数を呼び出し、次にその関数の応答で関数を呼び出して、この応答で電子メールの一意性をチェックすることです。ユーザーを作成するために ajax 要求が行われます。

最初の 1 つは、フォームの検証です。

function validateregForm()
{   

if($('#u_name').val()=="" || !IsEmail($('#u_email').val()) || $('#u_pwd').val().length<6 || $('#c_pwd').val()!=$('#u_pwd').val())
{   
    if($('#u_name').val()=="")
    {
        $('#reg_error1').show();    
    }
    if(!IsEmail($('#u_email').val()))
    {
        $('#email_msg').remove();
        $('#reg_error2').show();
    }
    if($('#u_pwd').val().length<6)
    {
        $('#reg_error3').show();
    }
    if($('#u_pwd').val()!=$('#c_pwd').val())
    {
        $('#reg_error4').show();
    }
    return false;

}
else
{
    return true ;
}

2 つ目は、ajax リクエストでメールの一意性を確認するためのものです。

function chkmail(email)             {
var posting=$.post('http://localhost/tv100.info/index.php/user/chkmail',{u_email:$('#u_email').val()});
posting.done(function(data){
if(data=='success')
    {
        $('#email_error').css('display','none');
        $('#email_msg').css('display','block');
        return true;
    }
    if(data=='failure')
    {
        $('#email_msg').css('display','none');
        $('#email_error').css('display','block');
        return false;
    }
});

}

3つ目は、これもajaxリクエストでユーザーを作成するためのものです。

$('#regform').submit(function(event)    {

var res=validateregForm()
event.preventDefault();
if(res)
{
    var email=chkmail();
}
if(email)
{
    $('#loading2').show();
var posting=$.post('http://localhost/tv100.info/index.php/user/create_user',$("#regform").serialize());
posting.done(function(data)
    {
    $('#loading2').hide();
     if(data=="success")
        {

        $('#reg_panel').append('<span id="reg_msg">Registration successful Now You are logged IN</span>');  
        $('#overlay').fadeOut(300);
        $('#login').html('Logout');
        $('#sign_in').hide();
        $('#cmmnt_field').show();
        }
    if(data=="failure")
        {
        $('#reg_panel').append('<span id="res_msg">Something Went Wrong try again  Latter</span>'); 
        }
    });
}
});
4

4 に答える 4

0

非同期および同期の概念について学ぶ必要があります。Ajax 呼び出しは通常非同期です。async各 ajax リクエストのパラメーターを false に設定すると、結果が得られます。ドキュメントから

async (default: true)
Type: Boolean
By default, all requests are sent asynchronously (i.e. this is set to true by default).
If you need synchronous requests, set this option to false.
Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation.
Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.
As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() or the deprecated jqXHR.success().
于 2013-09-06T05:54:01.290 に答える
0

電子メール検証の結果を処理するには、コールバックを使用する必要があります

function chkmail(email, callback) {
    var posting = $.post('http://localhost/tv100.info/index.php/user/chkmail', {
        u_email : email
    });
    posting.done(function(data) {
        if (data == 'success') {
            callback(true);
        } else if (data == 'failure') {
            callback(false);
        }

    });

}

$('#regform').submit(function(event) {

    var res = validateregForm()
    event.preventDefault();
    if (res) {
        chkmail($('#u_email').val(), function(valid) {
            if (valid) {
                $('#email_error').css('display', 'none');
                $('#email_msg').css('display', 'block');
                $('#loading2').show();
                var posting = $.post('http://localhost/tv100.info/index.php/user/create_user', $("#regform").serialize());
                posting.done(function(data) {
                    $('#loading2').hide();
                    if (data == "success") {
                        $('#reg_panel').append('<span id="reg_msg">Registration successful Now You are logged IN</span>');
                        $('#overlay').fadeOut(300);
                        $('#login').html('Logout');
                        $('#sign_in').hide();
                        $('#cmmnt_field').show();
                    }
                    if (data == "failure") {
                        $('#reg_panel').append('<span id="res_msg">Something Went Wrong try again  Latter</span>');
                    }
                });
            } else {
                $('#email_msg').css('display', 'none');
                $('#email_error').css('display', 'block');
            }
        });
    }
});
于 2013-09-06T05:54:18.487 に答える
0

validateregForm() 関数の最初の行にエラーがあります。

変化する

if($('#u_name').val=="" || !IsEmail($('#u_email').val()) 

if($('#u_name').val() =="" || !IsEmail($('#u_email').val())

                    ^ `.val()` here.
于 2013-09-06T05:50:56.180 に答える