Reputation: 87
In my code, when the button add is pressed, it creates a row in the table.
I need, in each line, when you leave the quantity or price input, the sum makes and assignes the value in the subtotal.
HTML
<table width="100%" border="0" >
<tr>
<td>Item</td>
<td>Quantity</td>
<td>U$ Price</td>
<td>Subtotal</td>
<td>Add</td>
<td>Remove</td>
</tr>
<tr class="tr_clone">
<td>
<select style="width:200px" name="itens[]">
<option value="0"></option>
<option value="1">Item A</option>
<option value="2">Item B</option>
<option value="3">Item C</option>
</td>
<td><input type="text" size="5" maxlength="5" autofocus name="qtd[]" class="text ui-widget-content ui-corner-all"></td>
<td><input type="text" size="10" maxlength="10" name="price[]" id="price" class="text ui-widget-content ui-corner-all"></td>
<td><input type="text" size="10" maxlength="10" name="subtotal[]" class="text ui-widget-content ui-corner-all"></td>
<td><input type="button" name="add" value="Add" class="tr_clone_add"></td>
<td><input type="button" name="remove" value="Remove" class="tr_clone_remove"></td>
</tr>
</table>
jQuery
var $to_clone = $('.tr_clone').first().clone();
$("table").on('click', 'input.tr_clone_add', function () {
var $tr = $(this).closest('.tr_clone');
var $clone = $to_clone.clone();
$clone.find(':text').val('');
$tr.after($clone);
});
$("table").on('click', 'input.tr_clone_remove', function () {
var $tr = $(this).closest('.tr_clone');
$tr.remove();
});
Upvotes: 1
Views: 1397
Reputation: 251
Try this. Should be what your looking for (fiddle)[http://jsfiddle.net/C6Su2/]:
HTML:
<table width="100%" border="0" >
<tr>
<td>Item</td>
<td>Quantity</td>
<td>U$ Price</td>
<td>Subtotal</td>
<td>Add</td>
<td>Remove</td>
</tr>
<tr class="tr_clone">
<td>
<select style="width:200px" name="itens[]">
<option value="0"></option>
<option value="1">Item A</option>
<option value="2">Item B</option>
<option value="3">Item C</option>
</td>
<td><input type="text" size="5" maxlength="5" name="qtd" class="quantity text ui-widget-content ui-corner-all"></td>
<td><input type="text" size="10" maxlength="10" name="price" class="price text ui-widget-content ui-corner-all"></td>
<td><input type="text" size="10" maxlength="10" name="subtotal" class="subtotal text ui-widget-content ui-corner-all"></td>
<td><input type="button" name="add" value="Add" class="tr_clone_add"></td>
<td><input type="button" name="remove" value="Remove" class="tr_clone_remove"></td>
</tr>
</table>
jQuery 1.3+ (jsfiddle):
var $to_clone = $('.tr_clone').first().clone();
$("table").on('click', 'input.tr_clone_add', function () {
var $tr = $(this).closest('.tr_clone');
var $clone = $to_clone.clone();
$clone.find(':text').val('');
$tr.after($clone);
});
$("table").on('click', 'input.tr_clone_remove', function () {
var $tr = $(this).closest('.tr_clone');
$tr.remove();
});
$(".quantity, .price").live("keyup", function(){
var prtCont = $(this).parent().parent();
var prce = parseInt(prtCont.find('.price').val()) - 0;
var qnty = parseInt(prtCont.find('.quantity').val()) - 0;
if(!isNaN(prce) && !isNaN(qnty)){
prtCont.find('.subtotal').val(prce * (qnty));
}else{
prtCont.find('.subtotal').val("");
}
});
OR jQuery 1.9+ (jsfiddle)
var $to_clone = $('.tr_clone').first().clone();
$("table").on('click', 'input.tr_clone_add', function () {
var $tr = $(this).closest('.tr_clone');
var $clone = $to_clone.clone();
$clone.find(':text').val('');
$tr.after($clone);
});
$("table").on('click', 'input.tr_clone_remove', function () {
var $tr = $(this).closest('.tr_clone');
$tr.remove();
});
$(document).on("keyup", ".quantity, .price", function(){
var prtCont = $(this).parent().parent();
var prce = parseInt(prtCont.find('.price').val()) - 0;
var qnty = parseInt(prtCont.find('.quantity').val()) - 0;
if(!isNaN(prce) && !isNaN(qnty)){
prtCont.find('.subtotal').val(prce * (qnty));
}else{
prtCont.find('.subtotal').val("");
}
});
Upvotes: 1
Reputation: 388316
If you can make the following changes to the html try the below code
qty
to the input element qtd
price
to the input element price
subtotal
to the input element subtotal
Code
$('table').on('change', '.qty, .price', function(){
var tr = $(this).closest('tr');
var prce = parseInt($('.price', tr).val(), 10);
var qnty = parseInt($('.qty', tr).val(), 10);
if(!isNaN(prce) && !isNaN(qnty)){
$('.subtotal', tr).val(prce * (qnty));
}else{
$('.subtotal',tr).val("");
}
})
Demo: Fiddle
Upvotes: 0