简体   繁体   English

用JavaScript计算更多数量更便宜的价格

[英]Calculate cheaper price by more quantity in javascript

I have created a form to calculate the price times the quantity of a item. 我创建了一个表格来计算价格乘以一件物品的数量。 It had more choices but I narrowed it down to one choice. 它有更多选择,但我将范围缩小到一个选择。 But now I can't figure out how give price breaks for more quantity. 但是现在我不知道如何给更多的价格打折。 Basically there are price breaks as follows: 基本上有如下价格突破:

 Item A is 4.60 for 1+ item. For 10+ items 3.40, 25+ 2.68 and so on until it hits 5000+ items.  This is the same for items, B and C except they are priced different. 

How can I calculate this using the method below: 如何使用以下方法计算此值:

Html Form: HTML形式:

<form action="#" id="price-quote" onsubmit="return false">
<table width="501" border="0" cellpadding="10" cellspacing="20" style="padding-  top:30px;">
<tr>
<th width="67" scope="row">Size:</th>
<td width="273" class="select-box">  <select id="size" name="size">
<option value="None">Select Size</option>
<option value="2.5 inches">2.5 inches</option>
<option value="3 inches">3 inches</option>
<option value="Oval">Oval</option>
</select>
</td>
 </tr>
<tr>
<th scope="row">Quanitity:</th>
<td><input type="text"  name="quantity" id="quantity" /></td>
</tr>
<tr>
 <th scope="row">&nbsp;</th>
<td><input class="button" type="button" value="Update" onmousedown="getTotal()"/></td>
</tr>
<tr>
<th> Price:</th>
<td><div id="totalPrice" style="float:right;"></div></td>
</tr>
</table>
</form>

Javascript: Javascript:

var size_prices= new Array();
size_prices["None"]=0;
size_prices["2.5 inches"]=4.60;
size_prices["3 inches"]=4.90;
size_prices["Oval"]=5.10;

function getSizePrice()
 {
var sizePrice;
var theForm = document.forms["price-quote"];
 var selectedSize = theForm.elements["size"];
sizePrice = size_prices[selectedSize.value];
return sizePrice;
}

function getQuantity()
{
var theForm = document.forms["price-quote"];
//Get a reference to the TextBox
var quantity = theForm.elements["quantity"];
var howmany =0;
//If the textbox is not blank
if(quantity.value!="")
{
    howmany = parseInt(quantity.value);
}
return howmany;
}

function getTotal()
{
var instantPrice = getSizePrice() * getQuantity(); 
document.getElementById('totalPrice').innerHTML =
                                  "$"+instantPrice.toFixed(2);
}

Could someone please point me in the right direction. 有人可以指出我正确的方向。 Thank you 谢谢

function getTotal()
{
    var q = getQuantity();
    var unitPrice =  4.60;
    if(q >= 10){
        unitPrice =  3.40;
    } else if (q >= 25){
        unitPrice =  2.68;
    }

    var instantPrice = unitPrice * q; 
    document.getElementById('totalPrice').innerHTML =
                              "$"+instantPrice.toFixed(2);
}

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

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