总和正确显示在所有浏览器中。现在,当我删除行时,总和的值会减少,但在 Firefox 浏览器中不能正确显示。假设 10 + 10 + 10 = 30 如果删除 2 行,则 sum 显示 20 。但这将是 10。出了什么问题。
<script>
$(document).ready(function () {
$("form").on('keyup', '.input_ttotal', function() {
calculateSum();
});
$('#btnDel').click(function () {
calculateSum();
});
});
function calculateSum() {
var sum = 0;
$(".input_ttotal").each(function () {
if (!isNaN(this.value) && this.value.length != 0) {
sum += parseFloat(this.value);
}
});
$("#sum").html(sum.toFixed(2));
}
</script>
html是
<form action="" method="POST">
<div class="yes">
<div id="cost1" class="clonedCost" style="display: inline;">
<table border="0">
<tr>
<td>
<label class="total" for="total">Total 1</label>
</td>
</tr>
<tr>
<input class="input_ttotal" id="ttotal" type="text" name="ttotal[]" />
</td>
</tr>
</table>
</div>
<div id="addDelButtons_cost">
<input type="button" id="btnAdd" value="+">
<input type="button" id="btnDel" value="-">
</div>
<p>Sum is:<span id="sum">0</span>
</p>
</div>
</form>
jsfiddle 链接是http://jsfiddle.net/ByLrz/1/