Javascript和<;形式行动>;;合计输入到文本框中的数字

Javascript和<;形式行动>;;合计输入到文本框中的数字,javascript,Javascript,这是我的密码: <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 ) {

这是我的密码:

<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>

var苹果价格=5;
var香蕉价格=10;
var orange_价格=15;
函数apple\u subtotal(num\u apple)//返回苹果的成本:
{
如果(苹果数>5){
返回苹果价格*num苹果*0.9;
}否则{
返回苹果价格*num\u appl;
}
}
函数banana\u subtotal(num\u banana)//返回香蕉的成本:
{
如果(数量>5){
返回香蕉价格*num\u香蕉*0.9;
}否则{
返回香蕉价格*香蕉数量;
}
}
函数orange\u subtotal(num\u orange)//返回橙子的成本:
{
如果(数值大于5){
返回橙色价格*num\u橙色*0.9;
}否则{
返回橙色价格*num\u橙色;
}
}
函数grand\u subtotal(num\u apple、num\u banana、num\u orange)//返回所有项目的总成本:
{
var a=苹果小计(苹果数量);
var b=香蕉小计(香蕉数量);
var c=橙色小计(橙色数量);
var d=苹果数+香蕉数+橙子数;
如果(d>10){
回报率(a+b+c)*1.07*0.95;
}否则{
回报率(a+b+c)*1.07;
}
}
苹果的数量:
香蕉数量:
橘子的数量:

总计:$
基本上,我写的代码是为我所在的一个类的虚拟在线商店编写的。如果你订购10件以上的商品,你可以得到5%的折扣。我的问题是我无法正确计算总数,相反,无论购买多少,折扣始终适用。有没有办法提取表单部分的信息,并将其调用到进行计算的汇总部分?

在您的帖子中,您会说“如果您订购超过10件商品,您将获得5%的折扣”,但在代码中您有:

if (num_apple > 5 ) {
  return apple_price * num_apple * 0.9;
}
var d = '1' + '0' + '0';  // '100'
<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)">
所以你给5件以上的商品打折,不是10件,给10%的折扣,不是5%。您可以使用以下方法简化此代码:

function apple_subtotal (num_apple) {
  return apple_price * num_apple * (num_apple > 10? 0.95 : 1);
}
但这将对整个订单应用10%的折扣,而不仅仅是那些超过10%的订单(也许这是你想要的,也许不是)

另一个问题是表单值始终是字符串,因此在执行此操作时:

var d = num_apple + num_banana + num_orange;
您正在连接这些值,而不是添加它们。因此,如果有人点了1个苹果、0个香蕉和0个橙子,那么你有:

if (num_apple > 5 ) {
  return apple_price * num_apple * 0.9;
}
var d = '1' + '0' + '0';  // '100'
<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)">
要使
+
运算符进行加法而不是串联,请将它们全部设置为数字。一些选择:

var d = +num_apple + +num_banana + +num_orange;

var d = Number(num_apple) + Number(num_banana) + Number(num_orange);

var d = parseInt(num_apple, 10) + parseInt(num_banana, 10) + parseInt(num_orange, 10);
在其他函数中没有此问题,因为它们不进行加法。使用减法、除法和乘法运算符,参数会首先转换为数字(如果可以的话),即使它们是字符串

此外,如果您有:

if (num_apple > 5 ) {
  return apple_price * num_apple * 0.9;
}
var d = '1' + '0' + '0';  // '100'
<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)">

请注意,表单控件具有引用其所在表单的表单属性,因此:

<input type="button" value="total" onclick="
  this.form.grand_total.value = grand_subtotal(this.form.apples.value, this.form.bananas.value, this.form.oranges.value)">

但在单独的函数中,而不是在线侦听器中,所有这些都会更好。

您可以尝试以下方法:

只需将grand_subtotal函数替换为以下内容:

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);
        alert(c);
        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;
        }
    }

您检查的总金额是多少??项目总数(香蕉+橘子+苹果)如果3个项目的总数超过10个,那么你想申请折扣吗?