Javascript Java脚本if语句不工作
我有一个javascript函数,它包含很少的Javascript Java脚本if语句不工作,javascript,jquery,Javascript,Jquery,我有一个javascript函数,它包含很少的if-else语句,因为某些原因其中一条语句不能正常工作 脚本: function makePayment(reservationID) { var expirationDate = $("#expirationDate").val(); var creditCard = $("#creditcard").val(); if (creditCard.length == 16 && expirationDate.length == 4
if-else语句
,因为某些原因其中一条语句不能正常工作
脚本:
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");
}
}
提前谢谢 请确保
currect
不为null或未定义,如果不是。
将“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
}
您必须将当前if/else放在ajax调用的success函数中。使用ajax时,
下面的并不意味着后面的。变量currect
在AJAX回调有机会将结果分配给它之前被检查。为什么如果currect==true??还有else if Just do if(currect){}else{}为什么要将currect与字符串“false”进行比较?这不是一个布尔值吗?@raina77ow所以一段时间(currect!=Null)可能会有帮助?或者有更好的方法吗?即使它是一个字符串,当然否则{…
就足够了?
if (currect === 'true') {
//your code
} else if(currect === 'false') {
//your code
}