我不知道为什么同样的值不正确[重复]

时间:2022-07-23 20:07:56

This question already has an answer here:

这个问题在这里已有答案:

function checkEmailValid(email,result)
    {
        if(email.val()==""){
            result.text("이메일을 입력하십시요.");
            return false;
        }
        else{
            var re_mail = /^([\w\.-]+)@([a-z\d\.-]+)\.([a-z\.]{2,6})$/;
            if(re_mail.test(email.val())){
                var res = false;
                $.ajax({
                    type:"GET",
                    url: "http://127.0.0.1:3000/auth/checkemail/"+email.val(),
                    timeout: 2000,
                    beforeSend: function(){
                        result.text("이메일 확인 중...");
                    },
                    complete: function(jqXHR){
                        if(jqXHR.status==202){
                            res = true;

                        }else{
                            result.text("이미 등록된 이메일입니다.");
                            res = false;    
                        }
                    },
                    success:function(data,textStatus,jqXHR){

                    },
                    fail: function(){

                    }                       
                });
                alert("step2 : " + res); ////////Here is Step2!!!
                alert("step3 : " + res); ///////Here is Step3!!!
                return res;

            }else{
                result.text("이메일 형식이 올바르지 않습니다.");
                return false;   
            }
        }
    }

Above function is for Email Validation:

以上功能用于电子邮件验证:

If the email is valid the function will return true, but oddly enough res of step2 is false and res of step3 is true. What is happening?

如果电子邮件有效,该函数将返回true,但奇怪的是,step2的res为false,step3的res为真。怎么了?

Step 2 value:

第2步价值:

我不知道为什么同样的值不正确[重复]

Step 3 value:

第3步价值:

我不知道为什么同样的值不正确[重复]

1 个解决方案

#1


0  

i will not suggest you for setting "async" to false because it is a bad practice but you can use callback function to perform your task on true condition

我不会建议你将“async”设置为false,因为这是一个不好的做法,但你可以使用回调函数在真实条件下执行你的任务

i will explain

我会解释

first of all add another parameter in your function like

首先在你的函数中添加另一个参数

function checkEmailValid(email,result,SuccessCallback)

and create a function with which will perform your task when ajax return true

并创建一个函数,当ajax返回true时,该函数将执行您的任务

like

function EmailIdIsUnique()
{
    //perform your code here
}

and call this callback funtion in ajax

并在ajax中调用此回调函数

like

complete: function(jqXHR){
                        if(jqXHR.status==202){
                         //run your code here 
                          if ($.isFunction(SuccessCallback))
                          {
                            result = successcallback.call(this);                        
                          }
                        }else{
                            result.text("이미 등록된 이메일입니다.");
                            res = false;    
                        }
                    },

#1


0  

i will not suggest you for setting "async" to false because it is a bad practice but you can use callback function to perform your task on true condition

我不会建议你将“async”设置为false,因为这是一个不好的做法,但你可以使用回调函数在真实条件下执行你的任务

i will explain

我会解释

first of all add another parameter in your function like

首先在你的函数中添加另一个参数

function checkEmailValid(email,result,SuccessCallback)

and create a function with which will perform your task when ajax return true

并创建一个函数,当ajax返回true时,该函数将执行您的任务

like

function EmailIdIsUnique()
{
    //perform your code here
}

and call this callback funtion in ajax

并在ajax中调用此回调函数

like

complete: function(jqXHR){
                        if(jqXHR.status==202){
                         //run your code here 
                          if ($.isFunction(SuccessCallback))
                          {
                            result = successcallback.call(this);                        
                          }
                        }else{
                            result.text("이미 등록된 이메일입니다.");
                            res = false;    
                        }
                    },