这是我的代码:
<script type = "text/javascript" >
var apple_price = 5;
var banana_price = 10;
var orange_price = 15;
function apple_subtotal (num_apple) //returns the cost of apples:
{
if (num_apple > 5 ) {
return apple_price * num_apple * 0.9;
} else {
return apple_price * num_appl;
}
}
function banana_subtotal (num_banana) //returns the cost of bananas:
{
if (num_banana > 5 ) {
return banana_price * num_banana * 0.9;
} else {
return banana_price * num_banana;
}
}
function orange_subtotal (num_orange) // Returns the cost of oranges:
{
if (num_orange > 5 ) {
return orange_price * num_orange * 0.9;
} else {
return orange_price * num_orange;
}
}
function grand_subtotal(num_apple, num_banana, num_orange) // returns the total cost of all items combined:
{
var a = apple_subtotal(num_apple);
var b = banana_subtotal(num_banana);
var c = orange_subtotal(num_orange);
var d = num_apple + num_banana + num_orange;
if (d > 10 ) {
return (a + b + c) * 1.07 * 0.95;
} else {
return (a + b + c) * 1.07;
}
}
</script>
<form action="">
Number of Apples: <input type="text" name="apples" size="3" /></br> <!-- Variable for number of apples -->
Number of Bananas: <input type="text" name="bananas" size="3" /></br> <!-- Variable for number of bananas -->
Number of Oranges: <input type="text" name="oranges" size="3" /></br> <!-- Variable for number of oranges -->
<input type="button" value="total" onclick="document.forms[0].grand_total.value = grand_subtotal(document.forms[0].apples.value, document.forms[0].bananas.value, document.forms[0].oranges.value)" /></br> <!-- Button to calculate the grand total -->
Grand Total: $<input type="text" name="grand_total" /> <!-- Grand total display -->
</form>
基本上,我正在编写的代码是针对我所在班级的假装在线商店。如果您订购超过 10 件商品,您将获得 5% 的折扣。我的问题是我无法正确计算总数,而是始终应用折扣,无论购买的金额是多少。有没有办法提取表单部分中的信息并将其调用到进行计算的总小计部分?