Javascript 计算表中选定项目的总计
我有3张桌子,每一张都有多行。我需要找到一种方法来计算某些列的总数。每一行都有一个复选框,所以基本上,当选中该复选框时,我需要能够将该行的值添加到总数中 我目前有这个,这是为每一列合计,我只是不知道如何做,只有当复选框被选中时,总数然后更新,如果取消选择从总数中删除 表格示例Javascript 计算表中选定项目的总计,javascript,jquery,html-table,Javascript,Jquery,Html Table,我有3张桌子,每一张都有多行。我需要找到一种方法来计算某些列的总数。每一行都有一个复选框,所以基本上,当选中该复选框时,我需要能够将该行的值添加到总数中 我目前有这个,这是为每一列合计,我只是不知道如何做,只有当复选框被选中时,总数然后更新,如果取消选择从总数中删除 表格示例 <table class="transfer-group-table table table-hover tablesorter"> <thead> <tr> &
<table class="transfer-group-table table table-hover tablesorter">
<thead>
<tr>
<th>Name</th>
<th>Invoice #</th>
<th>Invoice Amount</th>
<th>Order Status</th>
<th>Payment Menthod</th>
<th>Service Fee</th>
<th>Funding Fee</th>
<th>Delivery Date</th>
<th>Transfer Amount</th>
<th></th>
</tr>
</thead>
<tbody>
<tr id="2942">
<td>
<p>A Company Ltd</p>
</td>
<td>
<p>18602</p>
</td>
<td class="AmountLoaned">
<p>324.00</p>
</td>
<td>
<p>Completed </p>
</td>
<td>
<p>BACS</p>
</td>
<td class="ServiceCharge">
<p>0.04</p>
</td>
<td class="FeeAmount">
<p>4.54</p>
</td>
<td>
<p>May 29, 2015</p>
</td>
<td class="TransferAmount">
<p>2.50</p>
</td>
<td>
<input type="checkbox" class="totalamountcb">
</td>
</tr>
</tbody>
将using遍历到tr
,然后使用复选框检查复选框是否选中
if($(this).closest('tr').find(':checkbox').is(':checked')){
//Perform addition
}
完整代码
// Calculate the total invoice amount from selected items only
function calculateInvoiceTotals() {
var Sum = 0;
// iterate through each td based on class and add the values
$(".AmountLoaned").each(function() {
//Check if the checkbox is checked
if ($(this).closest('tr').find(':checkbox').is(':checked')) {
var value = $(this).text();
// add only if the value is number
if (!isNaN(value) && value.length != 0) {
Sum += parseFloat(value);
}
}
});
$('#TotalInvoiceAmt').text(Sum.toFixed(2));
};
// Calculate the total transfer amount from selected items only
function calculateTransferTotals() {
var Sum = 0;
$(".TransferAmount").each(function() {
//Check if the checkbox is checked
if ($(this).closest('tr').find(':checkbox').is(':checked')) {
var value = $(this).text();
// add only if the value is number
if (!isNaN(value) && value.length != 0) {
Sum += parseFloat(value);
}
}
});
$('#TotalTransferAmt').text(Sum.toFixed(2));
};
看起来不错,但似乎不想工作。它并没有添加2totals@thatuxguy,我只添加了$(这个)。最近的('tr')。查找(':checkbox')。是('checked')
条件rest是你的代码奇怪,它计算了总数,没有这个。@thauxguy,我的糟糕的错过:
在中是(':checked')
见真棒!非常感谢你的帮助
// Calculate the total invoice amount from selected items only
function calculateInvoiceTotals() {
var Sum = 0;
// iterate through each td based on class and add the values
$(".AmountLoaned").each(function() {
//Check if the checkbox is checked
if ($(this).closest('tr').find(':checkbox').is(':checked')) {
var value = $(this).text();
// add only if the value is number
if (!isNaN(value) && value.length != 0) {
Sum += parseFloat(value);
}
}
});
$('#TotalInvoiceAmt').text(Sum.toFixed(2));
};
// Calculate the total transfer amount from selected items only
function calculateTransferTotals() {
var Sum = 0;
$(".TransferAmount").each(function() {
//Check if the checkbox is checked
if ($(this).closest('tr').find(':checkbox').is(':checked')) {
var value = $(this).text();
// add only if the value is number
if (!isNaN(value) && value.length != 0) {
Sum += parseFloat(value);
}
}
});
$('#TotalTransferAmt').text(Sum.toFixed(2));
};