Avoid negative calculation

Asked

Viewed 68 times

1

Colleagues,

How would I make the cart calculation not negative. See below:

When I click the button - the quantity remains at 1, but the calculation continues for the negative:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

The code I’m using is:

 // subtrair quantidade
$('.value-minus').on('click', function(){
 var divUpd = $(this).parent().find('.value'), newVal = parseInt(divUpd.text(), 10)-1;
 if(newVal>=1) divUpd.text(newVal);
 var valorProduto = $(this).closest('tr').find('td[data-nome]').data('nome'); // Funcional
 trocar = valorProduto.replace(",",".");
 valorTTotal =  document.getElementById("total").innerHTML;
 trocarSTotal = valorTTotal.replace(",",".");
 valorTotal = trocarSTotal - trocar;

if(result == '0.00'){ resultado = result; }else{ resultado = ''; }
 document.getElementById("total").innerHTML = valorTotal.toFixed(2) + resultado;
 document.getElementById("subtotal").innerHTML = valorTotal.toFixed(2);
 $.ajax({
     type: "GET",
     dataType: 'json',
      data:{ valor: valorTotal.toFixed(2) },
     url: "atualizar-carrinho.php",
     success: function(resposta){
     }
  });
});

HTML

<td class="invert">
<div class="quantity">
<div class="quantity-select">

<div class="entry value-minus">&nbsp;</div>
<div class="entry value"><span>1</span></div>
<div class="entry value-plus active">&nbsp;</div>
</div>
</div>
</td>
  • 1

    It would not be easier just to disable the negative button when it reached 0 items?

  • Right, but how would I do that? Could you help me?

  • 1

    Post the html that generates this layout

  • I changed the post, put the code that generates the buttons, which are actually Divs and jquery that makes the subtraction effect.

1 answer

0

I managed to fix it. I created a parole by staying that way:

if(newVal >= 1){
     valorTotal = trocarSTotal - trocar;
}

newVal is the amount.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.