我想以数组形式显示值的数量。我有很多表格,只有一个名称是“价格[]”,并且表格上有每个值。我想计算“价格[]”形式的总值(value)。我仍然是 javascript 的初学者。我想用javascript来计算它。我编写的以下代码。
<html>
<body>
<form id="hitung" name="hitung">
price 1 <input type="text" name="price[]" class="price" value="1000"/><br>
price 2 <input type="text" name="price[]" class="price" value="3000"/><br>
price 3 <input type="text" name="price[]" class="price" value="2000"/><br>
price 4 <input type="text" name="price[]" class="price" value="1000"/><br>
price 5 <input type="text" name="price[]" class="price" value="3000"/><br><br>
total <input type="text" name="total" class="total"/>
</form>
</body>
</html>
我想再问一下,如果外部有新的输入,那么总数也会直接改变。怎么做。请帮帮我。谢谢
最佳答案
这是一个示例:http://jsfiddle.net/c4UyA/1/
var sum = 0;
$("form > input[name='price[]']").each(function() {
$this = $(this);
sum += parseInt($this.attr("value"));
});
$("form > input[name='total']").attr("value", sum);
关于javascript - 用javascript计算数组形式的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14062970/