Calculate the sum of 02 fields automatically

Asked

Viewed 75 times

0

I have the following fields:

inserir a descrição da imagem aqui

The student number comes from the database and I’m storing in a hidden field:

<input type="hidden" name="NumAlunos" id="numAlunos" value="<?php echo $visualizar->NumAlunos; ?>" class="form-control">

How would I multiply this field with the unit value and fill in the Revenue field?

<div class="form-group">
                <label for="valorDesconto">Valor Unitário:</label>
                <div class="input-group">
                  <div class="input-group-addon" style="background-color: #FAFAFA">
                    <i class="fa fa-usd"></i>
                  </div>
                    <input type="text" name="ValorUnitario" class="form-control pull-right" id="valor" maxlength="10">
                </div>
              </div>
              <div class="form-group">
                <label for="valorDesconto">Receita:</label>
                <div class="input-group">
                  <div class="input-group-addon" style="background-color: #FAFAFA">
                    <i class="fa fa-usd"></i>
                  </div>
                    <input type="text" name="ValorReceita" class="form-control pull-right" id="valorReceita" maxlength="10">
                </div>
              </div>
  • It depends on how you want it... when you type, when you leave the field, only when there are changes in the field and so on.

  • 1

    $("#valorReceita").val( Number($("#NumAlunos").val()) * Number($("#ValorUnitario").val()) )

  • Hi Everson. Will be leaving the Unit Value field.

1 answer

1


Using Jquery:

Use the Blur function, this function is activated when exiting the selected input..

$("#valor").blur(function(){
  var receita = $("#numAlunos").val() * $(this).val(); 
  $("#valorReceita").val(receita);
});

This should work

  • Thanks Ricardo. It worked!

Browser other questions tagged

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