当我单击复选框以删除表格行时,我希望我的计算更新总计。我在类似情况下通过简单地将计算总和函数添加到删除行事件来完成此操作,但我似乎无法在这种情况下使其工作。代码:
//Remove Table Row
$(document).on('click', 'input.removeItem', function(){
$(this).closest('tr').remove();
calculateSum();
calculateTotal();
});
// Sum Amt Collected
$(document).on('keyup', 'input.amtcollected', calculateSum);
function calculateSum() {
var sum = 0;
var currentTable = $(this).closest('table').attr('id');
$('#' + currentTable + ' input.amtcollected').each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);
}
});
//.toFixed() method will roundoff the final sum to 2 decimal places
$('#' + currentTable + ' input.sumamtcollected').val(sum.toFixed(2));
}
// Daily Collection Total
$(document).on('keyup', 'input.amtcollected', calculateTotal);
function calculateTotal() {
var sum = 0;
$('input.sumamtcollected').each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);
}
});
//.toFixed() method will roundoff the final sum to 2 decimal places
$('input.dailycollectionstotal').val(sum.toFixed(2));
}
我在这里创建了一个小提琴:http://jsfiddle.net/tmac187/pQ8WD/
我在calculateTotal()之后发出警报;并弹出警报。不知道为什么它不起作用。 jslint没有显示我能看到的任何注意事项。我尝试过firebug,但我还是比较新手,所以不能100%肯定我在那里寻找...
感谢您的帮助!
答案 0 :(得分:1)
一个小错误
//Remove Invoice Line
$(document).on('click', 'input.removeItem', function () {
var currentTable = $(this).closest('table').attr('id');
$(this).closest('tr').remove();
calculateTableSum(currentTable);
calculateTotal();
});
....
function calculateSum() {
var currentTable = $(this).closest('table').attr('id');
calculateTableSum(currentTable);
}
function calculateTableSum(currentTable) {
var sum = 0;
$('#' + currentTable + ' input.amtcollected').each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);
}
});
//.toFixed() method will roundoff the final sum to 2 decimal places
$('#' + currentTable + ' input.sumamtcollected').val(sum.toFixed(2));
}
演示:Fiddle
当var currentTable = $(this).closest('table').attr('id');
处理程序从calculateSum
处理remove
时,this
中的window
指向{{1}}对象而不是删除的项目。 / p>