I am trying to make an invoice script in which I need to perform some calculations. The user will enter the qty field and the price field. The system should multiply qty * price and display quantities in the field. The total amount should be indicated in the grant field. I tried to browse and do something below. It works fine with <span>, but when I give a text box, it does not work. I am very new to jquery and iam without getting where I am making a mistake. My html is as follows:
<table id="myTable">
<thead>
<tr><th>Product name</th><th>Qty</th><th>Price</th>
<th align="center"><span id="amount" class="amount">Amount</span> </th></tr>
</thead>
<tfoot>
<tr><td colspan="2"></td><td align="right"><span id="total" class="total">TOTAL</span> </td></tr>
</tfoot>
<tbody>
<tr>
<td>Product 1</td><td><input type="text" class="qty" name="qty"></td>
<td><input type="text" value="11.60" class="price"></td>
<td align="center"><input type="text" class="amount" id="amount"><span id="amount" class="amount">0</span></td>
</tr>
<tr><td>Product 2</td><td><input type="text" class="qty" name="qty"></td>
<td><input type="text" value="15.26" class="price"></td>
<td align="center"><input type="text" class="amount" id="amount"><span id="amount" class="amount">0</span></td></tr>
<tr><td></td><td></td><td></td><td><input type="text" class="total" id="total"></td></tr>
</tbody></table>
My jquery looks like this:
<script type='text/javascript'>
$(window).load(function(){
$(document).ready(function(){
update_amounts();
$('.qty').change(function() {
update_amounts();
});
});
function update_amounts()
{
var sum = 0.0;
$('#myTable > tbody > tr').each(function() {
var qty = $(this).find('.qty').val();
var price = $(this).find('.price').val();
var amount = (qty*price)
sum+=amount;
$(this).find('.amount').text(''+amount);
});
$('.total').text(sum);
}
});
</script>
Script Link
Or is there a better way to get closer to the same. Pls help me with this.
source
share