我需要根據價格矩陣計算窗簾價格,這取決於寬度和高度。如何從數組中計算而不是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