Javascript if语句不起作用

时间:2013-11-11 07:43:20

标签: javascript jquery

由于某种原因,我的javascript函数包含少量if else statements,其中一个语句无效。

脚本:

function makePayment(reservationID) {
var expirationDate = $("#expirationDate").val();
var creditCard = $("#creditcard").val();

if (creditCard.length == 16 && expirationDate.length == 4) {
    var month = "";
    var year = "";
    month += expirationDate.charAt(0);
    month += expirationDate.charAt(1);

    year += expirationDate.charAt(2);
    year += expirationDate.charAt(3);

    var monthInt = parseFloat(month);
    var yearInt = parseFloat(year);
    var currect;

    if (monthInt > 0 && monthInt < 13 && yearInt > 12 && yearInt < 24) {



        $.ajax({
            url: "CreditAuthentication",
            data: "type=reserve&creditCard=" + creditCard + "&expirationDate=" + expirationDate,
            success: function(responseText) {
                currect = responseText;
                console.log(responseText);
            }, error: function(xhr) {
                alert(xhr.status);
            }});

        if (currect == true) {

            //
            $.ajax({
                type: "POST",
                url: "paymentMethods",
                data: "type=make&reservationID=" + reservationID,
                success: function(msg) {
                    console.log("Paymentmade");
                    alert("Payment made");

                    //alert(CCA);
                    document.location = "index.jsp#reservation";
                }
            });
        } 

        else if(currect === "false") {
             alert("Please enter valid details. 3rd");
        }
        //
    } else {
        alert("Please enter valid details. 2nd");
    }

} else {
    alert("Please enter valid payment information. 1st");
}
}

我已经检查了FireBug,我的servlet的返回值是正确的,console.log(responseText);在控制台中显示响应。但是由于某种原因,这部分声明无效并且不显示警报:

if (currect == true) {

    $.ajax({
        type: "POST",
        url: "paymentMethods",
        data: "type=make&reservationID=" + reservationID,
        success: function (msg) {
            console.log("Paymentmade");
            alert("Payment made");
            document.location = "index.jsp#reservation";
        }
    });

} else if (currect === "false") {
    alert("Please enter valid details. 3rd");
} 

提前致谢!

2 个答案:

答案 0 :(得分:0)

请确保currect不为null或未定义,如果不是。 制作一个'当前'全局,以便它可以访问:

  var currect; //declare it here
  $.ajax({
            url: "CreditAuthentication",
            data: "type=reserve&creditCard=" + creditCard + "&expirationDate=" + expirationDate,
            success: function(responseText) {
                currect = responseText;
                console.log(responseText);
            }, error: function(xhr) {
                alert(xhr.status);
            }});

然后如果currect是布尔值,那么尝试:

if (currect) {
   //your code
} else {
   //your code
} 

如果是字符串:

if (currect === 'true') {
   //your code
} else if(currect === 'false') {
   //your code
}

答案 1 :(得分:0)

您必须将当前的if / else放在ajax调用的success函数中。