tl;dr 当字段为空或值为 0 时,此函数默认应用 20% 折扣,如何防止它从一开始就触发?
function longlost() {
var months = parseFloat(document.querySelector('#months').value);
var finalPrice = (getServicePrice() + extraPrices()) * getDiscountPercent(months);
document.getElementById("result").value = "€" + finalPrice;
}
我也尝试过,但没有运气:
function longlost() {
var months = parseFloat(document.querySelector('#months').value);
if (months.length==0)
return ;
var months = parseFloat(document.querySelector('#months').value);
var finalPrice = (getServicePrice() + extraPrices()) * getDiscountPercent(months);
document.getElementById("result").value = "€" + finalPrice;
}
最佳答案
// Check for invalid number or number not being positive
if (isNaN(months) || months <= 0) return;
关于javascript - 防止函数触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73961942/