使用jquery获取表中所有行的总和
我有表格数据。我需要通过对该表中使用jquery获取表中所有行的总和,jquery,html,Jquery,Html,我有表格数据。我需要通过对该表中rowtoal列的所有行求和来显示表sumTotal。但在我的循环中,它只取第一行,不包括第二行。 Html代码: <table class="view_job cust_view_job table-striped table text-center" style="width: 100%;border:1px solid #ccc"> <thead> <tr style="background:#2b2e7
rowtoal列
的所有行求和来显示表sumTotal
。但在我的循环中,它只取第一行,不包括第二行。
Html代码:
<table class="view_job cust_view_job table-striped table text-center" style="width: 100%;border:1px solid #ccc">
<thead>
<tr style="background:#2b2e76" ;="">
<th colspan="3" style="padding: 0;">
<p style="color:white">
Bundle Two
</p>
</th>
<th><p style="color:white" class="BundleB773423" id="B7734">Total : <span class="BundelRowTotalB7734">200</span></p></th>
</tr>
<tr>
<th style="width:5%;text-align: center">Qty</th>
<th style="width:5%;text-align: center">Days</th>
<th style="width:10%;text-align: center">Rate</th>
<th style="width:10%;text-align: center">Row Total</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" value="1" readonly="" autocomplete="off" name="qty[]" placeholder="Qty" class="form-control Qty">
</td>
<td>
<input type="text" value="2" autocomplete="off" name="days[]" placeholder="Days" class="form-control Days">
</td>
<td>
<input type="text" value="100.00" autocomplete="off" id="rate_23" name="rate[]" placeholder="Rate" class="form-control rate">
</td>
<td>
<input type="text" value="200.00" autocomplete="off" readonly="" name="row_total[]" id="row_totalB773423" placeholder="Row Total" class="form-control rowTotal">
</td>
</tr>
<tr>
<td>
<input type="text" value="1" readonly="" autocomplete="off" name="qty[]" placeholder="Qty" class="form-control Qty">
</td>
<td>
<input type="text" value="1" autocomplete="off" name="days[]" placeholder="Days" class="form-control Days">
</td>
<td>
<input type="text" value="200.00" autocomplete="off" id="rate_24" name="rate[]" placeholder="Rate" class="form-control rate">
</td>
<td>
<input type="text" value="200.00" autocomplete="off" readonly="" name="row_total[]" id="row_totalB773424" placeholder="Row Total" class="form-control rowTotal">
</td>
</tr>
</tbody>
</table>
小提琴演示:
我不明白我哪里做错了。有什么建议吗。
多谢各位
您的bundleId
第二次出错&您最初也必须调用run()
试试这个:-
var calcObject={
运行:函数(){
变量id=“”;
var sum=bundleSum=qtyVal=daysVal=rateVal=rowtall=calcVat=daysvalue=ratevalue=FinalRow=Bsum=0;
$(“.Qty”)。每个(功能){
//仅当值为number时添加
如果(!isNaN(this.value)&&this.value.length!=0){
qtyVal=parseFloat(此.value);
}
daysVal=$(this).closest('tr').find('td:eq(1)input[type='text']).val();
如果(!isNaN(daysVal)){
daysvalue=parseFloat(daysVal);
}
rateVal=$(this.closest('tr').find('td:eq(2)input[type='text']).val();
如果(!isNaN(rateVal)){
ratevalue=parseFloat(rateVal);
}
rowtotal=Math.round(parseFloat(qtyVal)*parseFloat(daysvalue)*parseFloat(ratevalue));
如果(!isNaN(行总数)){
FinalRow=parseFloat(rowtotal);
}
id=$(this.closest('tr').find('td:eq(3)input[type='text'])).attr('id');
$('#'+id).val(parseFloat(FinalRow).toFixed(2));
});
var BundelID=“”;
$(“.rowtottal”)。每个(函数(索引、值){
//仅当值为number时添加
如果(!isNaN(this.value)&&this.value.length!=0){
sum+=parseFloat(此.value);
}
var RowID=$(this.attr('id');
var后缀=RowID.match(/\d+/)[0];
如果(索引==0){
BundelID=$('.BundleB'+后缀).attr('id');
}
if(RowID.indexOf(BundelID)!=-1){
var BValue=$('#'+RowID).val();
如果(!isNaN(BValue)){
Bsum+=parseFloat(BValue);
}
}
$('.BundelRowTotal'+BundelID).html(parseFloat(sum).toFixed(2));
});
}
};
$(函数(){
//第一次也运行它
calcObject.run();
$(文档).on('keyup','.Days',函数(){
calcObject.run();
});
$(文档).on('keyup','Qty',函数(){
calcObject.run();
});
$(文档).on('keyup','.rate',函数(){
calcObject.run();
});
$('.rowTotal').change(函数(){
calcObject.run();
});
});代码>
包二
总数:200
数量
天
比率
行总数
学会调试代码:在.rowTotal中添加console.log(BundelID)
——对于第二行,它是未定义的。第一行尝试查找与773423
second773424
匹配的捆绑包,而捆绑包行是BundleB773423
,因此只有第一行匹配是真的!谢谢我在查that@5367683,您还需要调用run()
函数以获得正确的结果。首先,请查看我的回答。不要乞求名誉,它会降低您的声誉。
var calcObject = {
run: function () {
var id = '';
var sum =bundleSum=qtyVal=daysVal=rateVal=rowtotal =calcVat=daysvalue=ratevalue=FinalRow=Bsum=0;
$(".Qty").each(function () {
//add only if the value is number
if (!isNaN(this.value) && this.value.length != 0) {
qtyVal = parseFloat(this.value);
}
daysVal = $(this).closest('tr').find("td:eq(1) input[type='text']").val();
if (!isNaN(daysVal)) {
daysvalue = parseFloat(daysVal);
}
rateVal = $(this).closest('tr').find("td:eq(2) input[type='text']").val();
if (!isNaN(rateVal)) {
ratevalue = parseFloat(rateVal);
}
rowtotal = Math.round(parseFloat(qtyVal)*parseFloat(daysvalue)*parseFloat(ratevalue));
if (!isNaN(rowtotal)) {
FinalRow = parseFloat(rowtotal);
}
id = $(this).closest('tr').find("td:eq(3) input[type='text']").attr('id');
$('#'+id).val(parseFloat(FinalRow).toFixed(2));
});
var BundelID = '';
$(".rowTotal").each(function () {
//add only if the value is number
if (!isNaN(this.value) && this.value.length != 0) {
sum += parseFloat(this.value);
}
var RowID = $(this).attr('id');
var suffix = RowID.match(/\d+/)[0];
BundelID = $('.BundleB'+suffix).attr('id');
if(RowID.indexOf(BundelID) != -1){
var BValue = $('#'+RowID).val();
if (!isNaN(BValue)) {
Bsum += parseFloat(BValue);
}
}
$('.BundelRowTotal'+BundelID).html(parseFloat(Bsum).toFixed(2));
});
}
};
$(function () {
$(document).on('keyup', '.Days', function () {
calcObject.run();
});
$(document).on('keyup', '.Qty', function () {
calcObject.run();
});
$(document).on('keyup', '.rate', function () {
calcObject.run();
});
$('.rowTotal').change(function () {
calcObject.run();
});
});