Calculate Price in Javascript

Asked

Viewed 829 times

1

I have a table where will be calculated Quantity x Price in javascript. Following the rule of decimals.

At the moment I’m using the basic function, but it only works if you put integers or with "." separating(15.50). By separating by comma the calculation no longer works.

function calcular() {
    var cpqtde = $('#cpqtde').text();
    var cpvalor = $('#cpvalor').val();
    
    $('#cpliqu').text(cpqtde * cpvalor);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<b>Quantidade:</b>
<span id="cpqtde">10</span><br>
<b>Valor:</b> <input id="cpvalor" onblur="calcular()"  type="text"><br>

<b>Resultado:</b>
<div id="cpliqu"></div>

  • 1

    Put the necessary code to simulate the problem. What "the basic function"?

1 answer

4


Change the type of input for number which will operate with , or with . and, in addition, it will make it impossible for the user to enter invalid characters. I hope I understood the problem and helped.

function calcular() {
    var cpqtde = $('#cpqtde').text();
    var cpvalor = $('#cpvalor').val();
    
    $('#cpliqu').text(cpqtde * cpvalor);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<b>Quantidade:</b>
<span id="cpqtde">10</span><br>
<b>Valor:</b> <input id="cpvalor" onblur="calcular()"  type="number"><br>

<b>Resultado:</b>
<div id="cpliqu"></div>

Browser other questions tagged

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