我目前有一个隐藏和显示元素的表单,具体取决于用户输入的值。但由于某种原因,我的if语句似乎只适用于第一个if / else条件而不是底部3
Javascript:
var car1 = document.getElementById("motorbike");
var car2 = document.getElementById("smartCar");
var car3 = document.getElementById("largeCar");
var car4 = document.getElementById("motorhome");
var daysAmount = document.getElementById('my-number-days');
if (daysAmount.value >= '1' && daysAmount.value <= '5') {
car1.style.display = "inline-block";
} else {
$(car1).hide(400);
}
if (daysAmount.value >= '1' && daysAmount.value <= '10') {
car2.style.display = "inline-block";
} else {
$(car2).hide(400);
}
if (daysAmount.value >= '3' && daysAmount.value <= '10') {
car3.style.display = "inline-block";
} else {
$(car3).hide(400);
}
if (daysAmount.value >= '2' && daysAmount.value <= '15') {
car4.style.display = "inline-block";
} else {
$(car4).hide(400);
}
答案 0 :(得分:2)
将字符串解析为整数以便正确比较:
var daysAmountInt = parseInt(daysAmount.value);
if (daysAmountInt >= 1 && daysAmountInt <= 5) {
car1.style.display = "inline-block";
} else {
$(car1).hide(400);
}