我需要根据价格矩阵计算窗帘价格,这取决于宽度和高度。如何从数组中计算而不是if语句?Javascript价格矩阵数组
function checkPrice() {
var price = calculatePrice();
function calculatePrice(price) {
var width = parseInt(document.getElementById('customid0').value);
var height = parseInt(document.getElementById('customid1').value);
// width up to 100
if (width <= 100){
if (height <=50){
price = 67.22;
}
if (height >50 && height <=100){
price = 103.34;
}
if (height >100 && height <=130){
price = 133.11;
}
}
// width between 101 and 125
if (width > 100 && width <= 125){
if (height <=50){
price = 76.69;
}
if (height >50 && height <=100){
price = 113.01;
}
if (height >100 && height <=130){
price = 146.05;
}
}
// width between 126 and 150
if (width > 125 && width <= 150){
if (height <=50){
price = 83.69;
}
if (height >50 && height <=100){
price = 124.74;
}
if (height >100 && height <=130){
price = 161.28;
}
}
return price;
}
document.getElementById('product_addtocart_form').action = '<?php echo $this->getAddToCartUrl($_product) ?>&price='+price;
optionsPrice.changePrice('options', price);
optionsPrice.reload();
Price.changePrice(price);
}
不要使用小数钱。在数学完成后,将它乘以100并除以100。尝试添加0.02和0.01,看看为什么。 – Incognito
谢谢,会做。 – Gumtee
这是功课吗? – Incognito