为什么这个函数返回“undefined”? [英] Why is this function returning "undefined"?

查看:214
本文介绍了为什么这个函数返回“undefined”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单验证函数,它验证并检查用户名是否被采用(ajax)。



这就是我称之为代码的方式:

  function validate(){
if(usernameValid()){
return true;
}
else {
return false;
}

}

我也按顺序提醒函数以查看它返回的内容:

  alert(usernameValid()); 

问题在于alertbox在某些情况下会提示未定义,所以我的验证不会跳转到下一场,它挂起。
更具体地说,除了当长度=== 0时,它会在所有情况下都发出未定义的警告,然后它警告错误,这是正确的。



这里是usernameValid函数:

$ p $ 函数usernameValid(fld){
if($('#username') .val()。length!== 0){
$ .post(username_check.php,{
username:$('#username')。val()
},函数(响应){
响应=转义(响应);

if(response == 1){
$('#username_error_invalid')。show();
$('#username_error_empty')。hide();
$('#username_error_taken')。hide();
return false;
}
else if(response == 2){
$('#username_error_taken')。show();
$('#username_error_invalid')。hide();
$('#username_error_empty')。hide ();
返回false;
}
else {
$('#username_error_taken')。hide();
$('#username_error_invalid')。hide();
$('#username_error_empty')。hide();
返回true;
}
});
}
else {
$('#username_error_empty')。show();
$('#username_error_invalid')。hide();
$('#username_error_taken')。hide();
返回false;
}
}

有人知道什么是错的,也就是为什么代码返回在我测试响应变量的情况下未定义?



BTW:用户名工作的验证本身(当它应该等时显示错误),但它返回undefined ...



感谢

解决方案

这是一个异步调用:

  $。post(username_check.php,{.... 

因此, usernameValid 中的返回 / code>返回(因为它在运行时没有返回任何 ,它是 undefined )。



如果你有 return false; 你应该调用另一个函数,例如: userNameInvalidFunction(),还有一些其他的 true ...或者继续提交等。



是让你的AJAX调用synchr onous ...但我强烈建议不要这样做,因为它会在执行时完全锁定用户的浏览器。


I have a form-validation function which validates and also checks to see if the username is taken or not (ajax).

This is how I call the code:

 function validate(){
   if(usernameValid()){
      return true;
   }
   else {
      return false;
   }

 }

I also alert the function in order to see what it returns:

  alert(usernameValid());

The problem is that the alertbox alerts "Undefined" in some cases, so my validation wont jump to the next field, it hangs. To be more specific, it alerts "Undefined" in all cases except when the "length===0", then it alerts false which is correct.

Here is the usernameValid function:

    function usernameValid(fld) {
     if( $('#username').val().length !== 0 ) {
      $.post("username_check.php", {
        username: $('#username').val()
      }, function(response){
            response = escape(response);

            if(response==1){
             $('#username_error_invalid').show();
             $('#username_error_empty').hide();
             $('#username_error_taken').hide();
             return false;
            }
            else if(response==2){
                 $('#username_error_taken').show();
                  $('#username_error_invalid').hide();
                 $('#username_error_empty').hide();
                 return false;
            }
            else {
                 $('#username_error_taken').hide();
                  $('#username_error_invalid').hide();
                 $('#username_error_empty').hide();
                 return true;
            }
      });
     } 
     else{ 
         $('#username_error_empty').show();
         $('#username_error_invalid').hide();
         $('#username_error_taken').hide();
         return false;
     }
}

Anybody know what is wrong, ie why the code returns undefined in the cases where I test the "response" variable?

BTW: The validation itself for the username works (error is displayed when it should etc), but it returns undefined...

Thanks

解决方案

This is an asynchronous call:

$.post("username_check.php", {....

So the return statements in the callback happen long after usernameValid returns (and since it didn't return anything when it was run, it's undefined).

Where you have return false; you should call another function, for example: userNameInvalidFunction(), and something else for true...or just continue submitting, etc.

The alternative is to make your AJAX call synchronous...but I strongly recommend against this, since it'll completely lock up the user's browser while it executes.

这篇关于为什么这个函数返回“undefined”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆