How to use the if condition in jquery with variable usage?

Asked

Viewed 73 times

-1

I am trying to compare the selling price and the rate value over the selling price. example: the product costs 1467,73R$. my rate is 20%.In this case 20% above the price of the product would give 1761,276R$ I want that when entering the sale value(product price), the field cannot accept a Meno value that the sale price that should be 1761,276R$ why we added 20%. The error is that even typing a value above what was requested, the alert of which was typed a value less than the waiting continues to appear. I don’t know what I should do.Someone can help?

Now in Jquery:

$("#qty").click(function() {
  var taxa = $("#taxa").val(); //0,2
  var Price = $("#valor_tabela").val(); // 1467,73
  var PriceHot = $("#valor_venda").val(value).val();
  var totalTaxa = parseInt(taxa) * parseFloat(Price); //293,546
  var percent = parseFloat(totalTaxa) + parseFloat(Price); //293,546 + 1467,73 =1761,276

  if ($("#valor_venda").val() < percent) {

    $('.enableOnInput').prop('disabled', true);
    alert('O valor da venda é menor que o valor tabelado!');
  } else {
    $('.enableOnInput').prop('disabled', false);
    var value = $("#valor_venda").val();
    var primaryincome = $("#qty").val();
    var otherincome = $("#valor_venda").val(value).val();
    // Alterado o sinal de '+', para  '*'
    var totalincome = parseInt(primaryincome) * parseFloat(otherincome);
    $("#result").val(totalincome);
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="col-sm-3 ">
  <label>Valor Tabelado</label>
  <input type="text" class="form-control" name="valor_tabela" id="valor_tabela" value="1467,73" readonly>
  <input type="text" class="form-control" name="taxa" id="taxa" value="0,2" readonly>
</div>
<div class="col-sm-3">
  <label>Valor Venda</label>
  <input type="text" class="form-control" placeholder="Valor venda em R$" name="valor_venda" id="valor_venda">
</div>
<div class="col-sm-3 ">
  <label>Quantidade</label>
  <input type="number" class="form-control calculate" id="qty" name="quantidade" value="0">
</div>
<div class="col-sm-3 mt-2 ">
  <label>Sub Total</labe>
<input type="number" class="form-control"  id="result" name="sub_total"  readonly>
                              </div>


  • 1

    Already gets it wrong that monetary value should not be treated with parseFloat(), something like 1200,50 with parseFloat() will become 1200.

  • should be pasrInt() in case?

  • No, he’ll turn his value into a whole, use toLocaleString() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

  • 1

    @Developer.mozilla.org I tested by swapping everything in parsInt() and it worked only that of course I’ll have everything in one piece.but then I switched all variables with parseFloat() and it worked. thanks guys. you gave me a light to solve this problem and I thank you :)

  • 1

    I recommend you put your answer to the question so it won’t be closed, keep in mind that it can help other people

  • @Azzi yes, I just posted. only that can only accept the answer in two days.

Show 1 more comment

1 answer

0


the rate value was seeing as integer using parseint() so I switched to parsFloat and it all worked out fine.

$("#qty").click(function () {
       var taxa = $("#taxa").val();//0,2
       var Price = $("#valor_tabela").val();// 1467,73
       var totalTaxa = parseFloat(taxa) * parseFloat(Price);//293,546
       var percent = parseFloat(totalTaxa) + parseFloat(Price);//293,546 + 1467,73 =1761,276
  
        if( $("#valor_venda").val() < percent){
              
              $('.enableOnInput').prop('disabled', true);
              alert('O valor da venda é menor que o valor tabelado!');
        }
        else{
          $('.enableOnInput').prop('disabled', false);
          var value = $("#valor_venda").val();
          var primaryincome = $("#qty").val();
          var otherincome = $("#valor_venda").val(value).val();
          // Alterado o sinal de '+', para  '*'
          var totalincome = parseInt(primaryincome) * parseFloat(otherincome);
          $("#result").val(totalincome);
        }
      })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="col-sm-3 ">
  <label>Valor Tabelado</label>
  <input type="text" class="form-control" name="valor_tabela" id="valor_tabela" value="1467,73" readonly>
  <input type="text" class="form-control" name="taxa" id="taxa" value="0,2" readonly>
</div>
<div class="col-sm-3">
  <label>Valor Venda</label>
  <input type="text" class="form-control" placeholder="Valor venda em R$" name="valor_venda" id="valor_venda">
</div>
<div class="col-sm-3 ">
  <label>Quantidade</label>
  <input type="number" class="form-control calculate" id="qty" name="quantidade" value="0">
</div>
<div class="col-sm-3 mt-2 ">
  <label>Sub Total</labe>
<input type="number" class="form-control"  id="result" name="sub_total"  readonly>
                              </div>

Browser other questions tagged

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