函数并不总是返回值?

时间:2012-09-02 17:47:21

标签: javascript jquery

我正在尝试使这个ajax请求函数工作,但netbeans发出警告,以下函数并不总是返回值。任何人都可以帮忙。

function fpform(){
    var response='';
    var fpemail = $('#frgtpwd').val();
    //var fpemail = document.getElementById('frgtpwd').value;

    if (fpemail == ""){
        $('span#fperror').text("insert your emal address");
        //document.getElementById('fperror').innerHTML = "Insert your email address";
        return false;
    } else {
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (filter.test(fpemail)==false) { 
            $('span#fperror').text("Email address is not in valid format");
            //document.getElementById('fperror').innerHTML = "Email address is not in valid format";
            return false;
        } else {
            $("#loader").html('<img src="images/ajax-loader.gif" />');
            $.post("forgot_password_process.php", {
                email:fpemail
            }, function(response){
                response = response.trim();
            }).success(function () {
                if (response == 'yes'){
                    $("#fperror").html('<font color="green"><b>Your password has been reset now and emailed to you </b></font>');
                    $("#loader").hide('<img src="images/ajax-loader.gif" />');
                    return true;
                } else {
                    alert("your email address was not found");
                    $("#loader").hide('<img src="images/ajax-loader.gif" />');
                    $("#fperror").html('<font color="black"><b> Email address was not found in database!</b></font>');
                    return false;
                } 
            });
        }
    }
}

2 个答案:

答案 0 :(得分:1)

代码中的return true;语句未从fpform返回。而是从给予.success()的回调函数返回。到执行此函数时,外部函数fpform早已返回。使用ajax从函数“返回”的唯一方法是使用回调。


在我给你任何代码之前,你犯了很多其他的错误:

  1. 您的电子邮件正则表达式/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/在我的电子邮件地址上失败。 +也是一个有效的角色。考虑not validating email addresses with regex

  2. $("#loader").hide('<img src="images/ajax-loader.gif" />')不起作用。完全没有。您想要$("#loader").empty()

  3. 您在顶部声明的变量response会被您的一个匿名函数中的参数response所遮蔽,使response = response.trim()无效。


  4. function fpform(callback) {
        var fpemail = $('#frgtpwd').val();
    
        if (fpemail == ""){
            $('span#fperror').text("insert your email address");
            callback(false);
        } else {
            var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            if (filter.test(fpemail)==false) { 
                $('span#fperror').text("Email address is not in valid format");
                callback(false);
            } else {
                $("#loader").html('<img src="images/ajax-loader.gif" />');
                $.post("forgot_password_process.php", {
                    email:fpemail
                }).success(function(response) {
                    response = response.trim();
                    if (response == 'yes'){
                        $("#fperror").html('<font color="green"><b>Your password has been reset now and emailed to you </b></font>');
                        $("#loader").hide('<img src="images/ajax-loader.gif" />');
                        callback(true);
                    } else {
                        alert("your email address was not found");
                        $("#loader").hide('<img src="images/ajax-loader.gif" />');
                        $("#fperror").html('<font color="black"><b> Email address was not found in database!</b></font>');
                        callback(false);
                    } 
                }).error(function() { callback(false); });
            }
        }
    }
    

答案 1 :(得分:0)

您应该在$.post(...).success(...);

之后返回值