总计的问题

时间:2011-05-29 18:34:42

标签: javascript

这里是我用于子总计和总计的代码,但它给了我一个奇怪的结果。 PL。帮助

function totalprice()
{
    len = document.forms["form1"]["quantity[]"].length;

    for(i=0;i<len;i++){
        a = document.forms["form1"]["quantity[]"][i].value;
        b = document.forms["form1"]["price[]"][i].value
        c = a * b

        document.forms["form1"]["total[]"][i].value = c;
        sum = 0;
        for (b=0;b<len;b++){
            var d = document.forms["form1"]["total[]"][b].value;
            var sum = sum + d;
            document.forms["form1"]["grandtotal"].value = sum;
        }    
    }
}

这里是html代码

<form action="here.php" method="post" name="form1">
Quantity: <input name="quantity[]" size="10">Price: <input name="price[]" size="10" onblur="totalprice();">
Total: <input name="total[]" size="10" readonly=true><br>

Quantity: <input name="quantity[]" size="10">Price: <input name="price[]" size="10" onblur="totalprice();">
Total: <input name="total[]" size="10" readonly=true><br>

Quantity: <input name="quantity[]" size="10">Price: <input name="price[]" size="10" onblur="totalprice();">
Total: <input name="total[]" size="10" readonly=true><br>

Grand Total: <input name="grandtotal">


<input type="submit" value="Submit">
</form>

1 个答案:

答案 0 :(得分:1)

看起来“总计”循环仍然在“总”循环中,所以你要多次计算总计。