简体   繁体   English

在javascript函数中设置变量

[英]Setting the variable in javascript function

I am having a problem setting a value to a variable. 我在将值设置为变量时遇到问题。 My code is as below. 我的代码如下。

function fpform(){
var response_new = '';
var password_reset = "";
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";
    password_reset = 'no';
} 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";
         password_reset = 'no';
    } else {
        $("#loader").html('<img src="images/ajax-loader.gif" />');
        $.post("forgot_password_process.php", {
            email:fpemail
        }, function(response){
            response_new = response.trim();
        }).success(function () {
            if (response_new == 'yes'){ 
                $("#fperror").html('<font color="green"><b>Your password has been reset now and emailed to you </b></font>');
                $("#loader").empty();
                password_reset = 'yes';
            } else {
                $("#loader").empty();
                $("#fperror").html('<font color="black"><b> Email address was not found in database!</b></font>');
                password_reset = 'no'; 
            } 
        }); 
    }
}

if (password_reset == "yes"){
    alert(password_reset);
    return true;
} else {
    alert(password_reset);
    return true;
}
}

The last if condition is checking if the password_reset variable is set to yes or not and returns true or false depending on that. 最后一个if条件是检查password_reset变量是否设置为yes,并根据该变量返回true或false。 But the alert displays blank value of the password_reset variable. 但是警报显示password_reset变量的空白值。 I can't seem to find a problem behind this as the password_reset variable should get assigned before reaching the last if. 我似乎找不到任何问题,因为password_reset变量应该在到达最后一个if之前分配。 can anyone please suggest a solution. 任何人都可以建议解决方案。

Kind Regards 亲切的问候

$.post() is asynchronous, therefor the request hasn't completed when you alert the data. $.post()是异步的,因此当您提醒数据时,请求尚未完成。 For this to work, you need to call the alert within the success-callback as well, to make sure that you have the data before you try to alert it. 为此,您还需要在成功回调中调用警报,以确保在尝试警报之前已拥有数据。 Another option would be to use a synchronous AJAX-call (this is rarely a good option though). 另一种选择是使用同步AJAX调用(尽管这很少是一个好的选择)。

Update 更新资料

Not sure why your function return true/false, but for that to work, you would have to make a synchronous AJAX-call, otherwise the outer function will return long before the AJAX-call completes, and at that point you don't know what the response is. 不确定为什么您的函数返回true / false,但是要使其正常工作,您必须进行同步的AJAX调用,否则外部函数将在AJAX调用完成之前返回很长一段时间,此时您不知道反应是什么。

You should probably try to restructure your code to make the code that depend on the response of your outer function callable by the success-callback instead. 您可能应该尝试重组代码,以使依赖于外部函数响应的代码可以通过成功回调来调用。

I was gonna post the same thing as as Christofer. 我要发布与Christofer相同的内容。 Here's a fixed code sample. 这是一个固定的代码示例。 Just remove the last code block too. 只需删除最后一个代码块。

.success(function () {
        if (response_new == 'yes'){ 
            $("#fperror").html('<font color="green"><b>Your password has been reset now and emailed to you </b></font>');
            $("#loader").empty();
            password_reset = 'yes';
            alert 'yes';
            return true;
        } else {
            $("#loader").empty();
            $("#fperror").html('<font color="black"><b> Email address was not found in database!</b></font>');
            password_reset = 'no';
            alert 'no';
            return false;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM