admin管理员组文章数量:1200771
I have dynamically created a table
. There is one column empty on page load but it fills values when I choose the quantity of a dropdown menu(other column) and a default price value (other column). I want to add the total values created of this process using jQuery and display the total value.
Has anyone any ideas how can I do that?I have tried a lot but no results appeared.
I try this:
$(document).each("[id^=total]", function(event){
var num = this.id.slice(5);
var sum = 0;
var value = $('#total'+num).text();
sum += parseFloat(this.value);
$('#sum').text(sum);
});
HTML (where the result will be displayed):
<tr>
<th colspan="6" style="text-align:right">Total:</th>
<th colspan="2" style="text-align:center"><span id="sum"></span></th>
</tr>
HTML (id='qua2' is a dropdown menu from 1 to 20):
<tr>
<td><center><selectclass='choose'id='qua2'></select></center></td>
<td><center><spanid='yprice2'>101.10</span></center></td>
<td><center><spanid='total2'></span></center></td>
</tr>
NOTICE:
Column total
is created after table creation.I choose a value of dropdown and makes this process (value of dropdown * price).Result displayed in column total.
UPDATED (I use this script but the result in id='sum'
is NaN
) :
$(document).on('change', "[id^=qua]", function(event){
var num = this.id.slice(3);
var sum = 0;
var value = $("#qua"+num).val();
var yprc = $("#yprice"+num).text();
var amount = value * yprc;
amount = amount.toFixed(2);
var $sum = $("#sum");
var $total = $("#total"+num);
$total.html(amount);
$("[id^=total]").each(function(){
sum += parseFloat($(this).text());
});
$sum.text(sum);
});
I have dynamically created a table
. There is one column empty on page load but it fills values when I choose the quantity of a dropdown menu(other column) and a default price value (other column). I want to add the total values created of this process using jQuery and display the total value.
Has anyone any ideas how can I do that?I have tried a lot but no results appeared.
I try this:
$(document).each("[id^=total]", function(event){
var num = this.id.slice(5);
var sum = 0;
var value = $('#total'+num).text();
sum += parseFloat(this.value);
$('#sum').text(sum);
});
HTML (where the result will be displayed):
<tr>
<th colspan="6" style="text-align:right">Total:</th>
<th colspan="2" style="text-align:center"><span id="sum"></span></th>
</tr>
HTML (id='qua2' is a dropdown menu from 1 to 20):
<tr>
<td><center><selectclass='choose'id='qua2'></select></center></td>
<td><center><spanid='yprice2'>101.10</span></center></td>
<td><center><spanid='total2'></span></center></td>
</tr>
NOTICE:
Column total
is created after table creation.I choose a value of dropdown and makes this process (value of dropdown * price).Result displayed in column total.
UPDATED (I use this script but the result in id='sum'
is NaN
) :
$(document).on('change', "[id^=qua]", function(event){
var num = this.id.slice(3);
var sum = 0;
var value = $("#qua"+num).val();
var yprc = $("#yprice"+num).text();
var amount = value * yprc;
amount = amount.toFixed(2);
var $sum = $("#sum");
var $total = $("#total"+num);
$total.html(amount);
$("[id^=total]").each(function(){
sum += parseFloat($(this).text());
});
$sum.text(sum);
});
Share
Improve this question
edited Dec 15, 2016 at 16:07
GeoDim
asked Dec 15, 2016 at 14:47
GeoDimGeoDim
1812 gold badges3 silver badges11 bronze badges
4
- 3 Could you post some example html please. – George Commented Dec 15, 2016 at 14:49
- $('#total'+num).val(); tried? – Vel Commented Dec 15, 2016 at 14:51
- Yes i did but still the same. – GeoDim Commented Dec 15, 2016 at 14:59
- I updated my question! – GeoDim Commented Dec 15, 2016 at 15:05
2 Answers
Reset to default 16Working fiddle.
What I suggest is to avoid the use of id's and use classes instead :
calc_total();
$(".choose").on('change', function(){
var parent = $(this).closest('tr');
var price = parseFloat($('.price',parent).text());
var choose = parseFloat($('.choose',parent).val());
$('.total',parent).text(choose*price);
calc_total();
});
function calc_total(){
var sum = 0;
$(".total").each(function(){
sum += parseFloat($(this).text());
});
$('#sum').text(sum);
}
Hope this helps.
calc_total();
$(".choose").on('change', function(){
var parent = $(this).closest('tr');
var price = parseFloat($('.price',parent).text());
var choose = parseFloat($('.choose',parent).val());
$('.total',parent).text(choose*price);
calc_total();
});
function calc_total(){
var sum = 0;
$(".total").each(function(){
sum += parseFloat($(this).text());
});
$('#sum').text(sum);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border=1>
<tr>
<td>
<center>
<select class='choose'>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
</select>
</center>
</td>
<td>
<center>
<span class='price'>100</span>
</center>
</td>
<td>
<center>
<span class='total'>100</span>
</center>
</td>
</tr>
<tr>
<td>
<center>
<select class='choose'>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
</select>
</center>
</td>
<td>
<center>
<span class='price'>100</span>
</center>
</td>
<td>
<center>
<span class='total'>100</span>
</center>
</td>
</tr>
<tr>
<td>
<center>
<select class='choose'>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
</select>
</center>
</td>
<td>
<center>
<span class='price'>100</span>
</center>
</td>
<td>
<center>
<span class='total'>100</span>
</center>
</td>
</tr>
<tr>
<th colspan="2" style="text-align:right">Total:</th>
<th colspan="2" style="text-align:center"><span id="sum"></span></th>
</tr>
</table>
Try this way:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sum Total for Column in jQuery </title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$('table thead th').each(function (i) {
calculateColumn(i);
});
});
function calculateColumn(index) {
var total = 0;
$('table tr').each(function () {
var value = parseInt($('td', this).eq(index).text());
if (!isNaN(value)) {
total += value;
}
});
$('table tfoot td').eq(index).text('Total: ' + total);
}
</script>
</head>
<body>
<table id="sum_table" width="300" border="1">
<thead>
<tr>
<th>ITEM 1</th>
<th>ITEM 2</th>
<th>ITEM 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Total:</td>
<td>Total:</td>
<td>Total:</td>
</tr>
</tfoot>
</table>
</body>
</html>
Hope it helps.
本文标签: javascriptSum values of a table column using jQueryStack Overflow
版权声明:本文标题:javascript - Sum values of a table column using jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738580088a2101111.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论