-1

由于某种原因,我的 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");
} 

提前致谢!

4

2 回答 2

0

请确保currect不为空或未定义,如果不是。制作一个“正确的”全局,以便可以访问它:

  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
}
于 2013-11-11T07:49:49.897 回答
0

您必须将正确的 if/else 放在 ajax 调用的成功函数中。

于 2013-11-11T07:50:46.973 回答