當我單擊複選框刪除表格行時,我希望我的計算更新彙總。我在類似的情況下通過簡單地將計算總和函數添加到移除行事件來完成此操作,但似乎無法在這種情況下使用它。刪除輸入字段更新
$(function() {
// Append Invoice Line
$(document).on('click', '#addnewitem', function() {
var currentTable = $(this).closest('table').attr('id');
$('#' + currentTable).append('<tr><td><div class="col-sm-6 col-lg-12"><input type="Client Name" class="form-control" id="c_name" placeholder="Item"></div></td><td><div class="col-sm-6 col-lg-12"><input type="Client Name" class="form-control" id="c_name" placeholder="Description"></div></td><td><div class="col-sm-6 col-lg-12"><input type="Client Name" class="form-control price" id="item_price" placeholder="Item Price" name="item_price[]"></div></td><td><button type="button" id="removeItem" class="btn btn- default"><span class="glyphicon glyphicon-trash"></span></td></tr>');
});
//Remove Invoice Line
$(document).on('click', '#removeItem', function() {
calculateTotal();
});
// Sum Amt Collected
function calculateSum() {
var sum = 0;
var currentTable = $(this).closest('table').attr('id');
$('#' + currentTable + ' input#item_price').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));
}
$(document).on('keyup', 'input#item_price', calculateSum);
});
你可以爲此做jsfiddle嗎?或者給演示頁面? – nick4fake
您能否提供一些HTML代碼樣本? – Kyle