Calculation using filled fields jQuery

1

I have the following fields:

moeda, taxa_compra, taxa_venda, valor_reais, valor_total

I need you to calculate, when filling sales tax,

taxa_venda / valor_reais e represente o total no valor_total.

How can I set up a jquery to do this?

    
asked by anonymous 04.03.2018 / 15:44

1 answer

1
  

How can I set up a jquery to do this?

You can use an event handler of type input or blur , for example:

$("seletor_do_campo_taxa_de_venda").on("input ou blur", function(){
   // ação
});

The input will execute action as some value is inserted or removed from the field; the% exits from the field when the field loses focus (the cursor exits the field).

To perform the sales_account / real_value division, the values of both fields must be blur . If the fields are of type number , you must convert them to text .

In order to play the value in the total_value field, put the result of the division into action :

// se os campos forem tipo number
$("seletor_do_campo_taxa_de_denda").on("input ou blur", function(){
   var total = $("seletor_do_campo_taxa_de_venda").val() / $("seletor_do_campo_valor_total").val();
   $("seletor_do_campo_valor_total").val(total);
});

or

// se os campos forem tipo text com decimais separados por vírgula
$("seletor_do_campo_taxa_de_venda").on("input ou blur", function(){
    var tx_venda = parseFloat($("seletor_do_campo_taxa_de_venda").val().replace(".","").replace(",","."));
    var vl_total = parseFloat($("seletor_do_campo_valor_total").val().replace(".","").replace(",","."));
    var total = tx_venda / vl_total;
    $("seletor_do_campo_valor_total").val(total);
});

Note: If the element representing the total_value is a number , change div by $("seletor_do_campo_valor_total").val(total);

    
04.03.2018 / 16:45