Рассчитать более дешевую цену на большее количество в JavaScript - PullRequest
0 голосов
/ 06 февраля 2012

Я создал форму для расчета цены, умноженной на количество товара. У него было больше вариантов, но я сузил выбор до одного. Но сейчас я не могу понять, как дать ценовые разрывы для большего количества. В основном ценовые разрывы следующие:

 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. 

Как я могу рассчитать это, используя метод ниже:

Форма 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:

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);
}

Может ли кто-нибудь, пожалуйста, указать мне в правильном направлении. Спасибо

1 Ответ

3 голосов
/ 06 февраля 2012
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);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...