1
I’m using a plugin that when I click on the button + it multiplies the value.
It works perfectly but the calculation is only done inside the <span id="price" class="amount"></span>
and brings the Total within the <span id="total" class="total_amount"></span>
When I try to put this interaction inside Input it doesn’t work.
function calculate(obj){
var price = parseFloat($(obj).parent().parent().parent().find('.amount').text()) || 0;
var quantity = parseInt($(obj).parent().find('.qty').val());
var total = price * quantity;
$(obj).parent().parent().parent().find('.total_amount').text(total);
}
function changeQuantity(num,obj){
$(obj).parent().find('.qty').val( parseInt($(obj).parent().find('.qty').val())+num);
}
$().ready(function(){
//calculate();
$(".minus").click(function(){
changeQuantity(-1,this);
calculate(this);
});
$(".plus").click(function(){
changeQuantity(1,this);
calculate(this);
});
$(".qty").keyup(function(e){
if (e.keyCode == 38) changeQuantity(1,this);
if (e.keyCode == 40) changeQuantity(-1,this);
calculate(this);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table cart">
<thead>
<tr>
<th class="cart-product-thumbnail"> </th>
<th class="cart-product-name">Produto</th>
<th class="cart-product-quantity">Quantidade</th>
<th class="cart-product-price">Precço Unitario</th>
<th class="cart-product-subtotal">Total</th>
</tr>
</thead>
<tbody>
<tr class="cart_item">
<td class="cart-product-thumbnail"></td>
<td class="cart-product-name">
<a href="#">Pera</a>
</td>
<td class="cart-product-quantity">
<div class="quantity clearfix">
<input type="button" value="-" class="minus" field="quantity">
<input type="text" id="quantity" name="quantity" value="1" class="qty" />
<input type="button" value="+" class="plus" field="quantity">
</div>
</td>
<td class="cart-product-price">
R$ <span id="price" class="amount">50000</span>
</td>
<td class="cart-product-subtotal">
<span id="total" class="total_amount"></span>
</td>
</tr>
<tr class="cart_item">
<td class="cart-product-thumbnail"></td>
<td class="cart-product-name">
<a href="#">Uva</a>
</td>
<td class="cart-product-quantity">
<div class="quantity clearfix">
<input type="button" value="-" class="minus" field="quantity">
<input type="text" id="quantity" name="quantity" value="1" class="qty" />
<input type="button" value="+" class="plus" field="quantity">
</div>
</td>
<td class="cart-product-price">
R$ <span id="price" class="amount">40000</span>
</td>
<td class="cart-product-subtotal">
<span id="total" class="total_amount"></span>
</td>
</tr>
<tr class="cart_item">
<td class="cart-product-thumbnail"></td>
<td class="cart-product-name">
<a href="#">Teste 3</a>
</td>
<td class="cart-product-quantity">
<div class="quantity clearfix">
<input type="button" value="-" class="minus" field="quantity">
<input type="text" id="quantity" name="quantity" value="1" class="qty" />
<input type="button" value="+" class="plus" field="quantity">
</div>
</td>
<td class="cart-product-price">
R$ <span id="price" class="amount">35000</span>
</td>
<td class="cart-product-subtotal">
<span id="total" class="total_amount"></span>
</td>
</tr>
<tr class="cart_item">
<td class="cart-product-thumbnail">
</td>
<td class="cart-product-name">
<a href="#">Teste 4</a>
</td>
<td class="cart-product-quantity">
<div class="quantity clearfix">
<input type="button" value="-" class="minus" field="quantity">
<input type="text" id="quantity" name="quantity" value="1" class="qty" />
<input type="button" value="+" class="plus" field="quantity">
</div>
</td>
<td class="cart-product-price">
R$ <span id="price" class="amount">35600</span>
</td>
<td class="cart-product-subtotal">
<span id="total" class="total_amount"></span>
</td>
</tr>
</tbody>
</table>
What do you mean? What input? Is there a way you can put the structure you want into Jsfiddle, and show where you want the interaction to work?
– JuniorNunes
@Juniornunes I created another Input Field just to show how would like the interaction
– Shaolin Fantastic
https://jsfiddle.net/felipefranco/fo5bzfpc/1/
– Shaolin Fantastic
you want to, by changing the value of
input
quantity update the total value?– Marllon Nasser
That’s right I’d like you to change the value of the total but I’m not getting it done
– Shaolin Fantastic
I just modified the calculate, see if this is what you want: https://jsfiddle.net/fo5bzfpc/2/
– JuniorNunes