简体   繁体   English

根据价格计算层数

[英]Calculate the quantity on layer based on price

I want to calculate the layer cost of making a cake based on option from the customer. 我想根据客户的选择来计算制作蛋糕的层成本。 1 layer cost 320.00 ( set price for 1 layer already) 1层成本320.00(已经为1层设置了价格)

Additional layer cost 100 附加层成本100

In html i have this: 在HTML我有这个:

<select id="layer" name="layer" onchange="updateTotal()" >
<?php   for( $i=1; $i<100; $i++ ) {
        echo "<option value=$i> $i </option>\n"; }
?> 
</select>

In my javascript I have this: 在我的JavaScript我有这个:

function updateTotal(){ 
var optionPrice = 0; 
function cakeLayerPrice(){
          document.getElementById('layer');
          optionPrice += 100;}
}
cakeLayerPrice();

var totalPrice = optionPrice;
document.getElementById('totalPrice').innerHTML = "$ " + totalPrice;
}

Apparently the amount won't display if I choose even 1 layer, in my function cakeLayerPrice(), what is the right solution? 显然,如果我在函数cakeLayerPrice()中选择了仅一层,则金额将不会显示,什么是正确的解决方案?

eg it will display if I choose 例如,如果我选择它将显示

1 layer = 320.00 1层= 320.00

2 layers = 420.00 (+100) 2层= 420.00(+100)

3 layers = 520.00 (+100) and so on. 3层= 520.00(+100),依此类推。

Each time you're calling updateTotal() you're resetting optionPrice to 0. 每次调用updateTotal()时 ,都会将optionPrice重置为0。

Instead you should get from layer select box the layer number and multiply it to 100. 相反,您应该从图层选择框中获取图层编号并将其乘以100。

function updateTotal(){ 
    var layers = document.getElementById('layer');
    var totalPrice = parseInst(layers.value, 10) * 100;

    document.getElementById('totalPrice').innerHTML = "$ " + totalPrice;
}

If nothing is selected, or you did't set a default selected, you could get a -1 value. 如果未选择任何内容,或者未设置默认值,则将获得-1值。 In this case, just update the code to handle the negative values: 在这种情况下,只需更新代码以处理负值即可:

function updateTotal(){ 
    var layers = document.getElementById('layer');
    var totalPrice = parseInst(layers.value, 10) * 100;

    if (totalPrice > 0) {
        document.getElementById('totalPrice').innerHTML = "$ " + totalPrice;
    }
}

This example, avoids even the case your price is 0. 此示例避免了价格为0的情况。

If you have this possibility, just change the code as needed. 如果有这种可能,只需根据需要更改代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM