See this code. when I type some numbers in the .price
and .qty
inputs, if actually the t
variable is 100
, I get 0100
in the .total
input. Why there is a 0 in the first and how can I solve it?
$('.detail').delegate('.quantity, .price, .discount', 'keyup', function() {
var tr = $(this).parent().parent();
var qty = tr.find('.quantity').val();
var price = tr.find('.price').val();
var amt = qty * price;
tr.find('.amount').val(amt);
total();
});
function total() {
var t = 0;
$('.amount').each(function(i, e) {
var amt = $(this).val();
t += amt;
});
$('.total').html(t);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody class="detail">
<tr class="row">
<td>
<input type="text" class="no" value="1">
</td>
<td>
<input type="text" class="pid">
</td>
<td>
<input type="text" class="productname">
</td>
<td>
<input type="text" class="price">
</td>
<td>
<input type="text" class="quantity">
</td>
<td>
<input type="text" class="amount">
</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td><span class="total"></span></td>
</tr>
</tbody>
</table>
SEE THE FIDDLE: https://jsfiddle.net/z5d421at/