Good day.

Interested in this question: how to calculate the amount (total price) depending on the specific price and quantity entered in the input field? There is a code, how to change the value in the field, but here it’s impossible to calculate the amount as it is correct.

jQuery

//button input var sum = parseInt($('.sum_price').text()); $('.degr').click(function(){ var vI = $(this).parent().find('input').val(); if(vI > 0){ vI --; $(this).parent().find('input').val(vI); } var pr_m = parseInt($(this).parent().parent().find($('.price_sp')).text())*vI; }); $('.incr').click(function(){ var vI = $(this).parent().find('input').val(); vI++; $(this).parent().find('input').val(vI); var pr_p = parseInt($(this).parent().parent().find($('.price_sp')).text())*vI; }); 

I wrote jQuery myself, can someone come in handy. There is no particular experience :)

HTML

 <p>Стоимость заказа: <span class="sum_price">0</span> руб.</p> <p>Детали заказа:</p> <table class="form_goods_order"> <tr> <td class="name_goods">Набор экономного болельщика</td> <td class="price_goods"><span class="price_sp">695</span> р.</td> <td class="count"><img src="images/count_left.gif" title="Уменьшить" alt="Уменьшить" class="degr"/><input type="text" name="nab1" id="nab1" value="0" /><img src="images/count_right.gif" title="Увеличить" alt="Увеличить" class="incr" /></td> </tr> 

An example of working on the site , if you click on the buy button.

    1 answer 1

     var sum=0; $('.form_goods_order tr').each(function(){ sum += parseInt($(this).find('.price_sp').text())*parseInt($(this).find('input').val()); }); alert(sum); 

    upd:

     function get_sum(){ var sum=0; $('.form_goods_order tr').each(function(){ sum += parseInt($(this).find('.price_sp').text())*parseInt($(this).find('input').val()); }); return sum; } //button input $('.degr').click(function(){ var $t=$(this).parent().find('input'); var vI = Math.abs(parseInt($t.val())); $t.val(vI?--vI:0); $('.sum_price').html(get_sum()); }); $('.incr').click(function(){ var $t=$(this).parent().find('input'); var vI = Math.abs(parseInt($t.val())); $t.val(vI?++vI:1); $('.sum_price').html(get_sum()); }); 
    • not that. it is necessary to dynamically change the amount by clicking on the buttons more and less - SnowMan
    • duck wrap this code in a function and call it every time you click. - FLK
    • @SnowMan something like this - FLK
    • THX. works) - SnowMan