Give your table and “Get Total” button an ID:
<table id="cart">
<input id="calculateTotal" type="button" value="Get Total" />
Put this script in the <head>
of your page:
$(function() {
$('#calculateTotal').click(function() {
var total = 0;
$('#cart tr:gt(0)').each(function() {
total +=
parseFloat($(this).find('td:eq(3)').text()) *
parseFloat($(this).find('input:last').val());
});
// display total in textbox
$('#total').val(total);
});
});
If you want to restrict users to entering whole numbers only (you can’t buy a fraction of soap, for example), add this to your jQuery ready function $(function() {
:
$('#cart').on('keyup', '.toAdd', function() {
$(this).val( $(this).val().replace(/[^0-9]/, '') );
});
To format your cart total to two decimal places:
total = parseInt(total * 100) / 100;
1
solved calculate the dynamic values of rate * quantity [closed]