我有下面的代碼不工作。jQuery邏輯運算符沒有按預期在jquery中工作
var day = $("#day").val();
var week = $("#week").val();
var month = $("#month").val();
if (day > week) {
alert(day > week);
$('#error').text("Day Limit Must be smaller than Week Limit");
$('.alert-danger').show();
setTimeout(function() {
$(".alert-danger").hide();
}, 5000);
return false;
}
if (day > month) {
$('#error').text("Day Limit Must be smaller than Month Limit");
$('.alert-danger').show();
setTimeout(function() {
$(".alert-danger").hide();
}, 5000);
return false;
}
if (month < week) {
$('#error').text("Week Limit Must be smaller than Month Limit and greater than Day Limit");
$('.alert-danger').show();
setTimeout(function() {
$(".alert-danger").hide();
}, 5000);
return false;
}
這裏我得到day=4
和week=6
和month=9
。在這種情況下,所有if
條件都正常工作。但month=10
,第二個if
條件不起作用(4>10)
實際上是false
但它變得true
。
我的邏輯有什麼問題?
'.VAL()'返回字符串前值到整數轉換,則需要將其轉換爲數字 – Satpal
你比較** **字符串。在這裏有一個重複的地方...(在正常情況下'val'返回一個字符串[異常情況:一個空的jQuery對象或jQuery對象中的非字段元素,在這種情況下,您將獲得'undefined']。) –