Add values from jquery or javascript fields

Asked

Viewed 52 times

0

I have the following form below:

inserir a descrição da imagem aqui

How would I multiply the values according to the quantities and the total value automatically appears in the Total Value field? Below is the HTML code:

<form class="" action="#!" method="post">
  <div class="form-group">
    <label>Quantidade de adultos:</label>
    <input type="number" class="form-control" name="QtdAdultos" value="">
  </div>
  <div class="form-group">
      <label>Valor por adulto:</label>
      <input type="text" name="ValorAdulto" class="form-control" id="valorAdulto" maxlength="12" required>
  </div>
  <div class="form-group">
    <label>Quantidade de crianças até 6 anos:</label>
    <input type="number" class="form-control" name="QtdCriancas6Anos" value="">
  </div>
  <div class="form-group">
      <label>Valor por criança até 6 anos:</label>
      <input type="text" name="ValorCrianca6Anos" class="form-control" id="valorCrianca6a12" maxlength="12">
  </div>
  <div class="form-group">
    <label>Quantidade de crianças entre 6 e 12 anos:</label>
    <input type="number" class="form-control" name="QtdCriancas6a12Anos" value="">
  </div>
  <div class="form-group">
      <label>Valor criança até 6 a 12 anos:</label>
      <input type="text" name="ValorCrianca6a12" class="form-control" id="valorCrianca6a12" maxlength="12">
  </div>
  <div class="form-group">
    <label>Quantidade de adolescentes:</label>
    <input type="number" class="form-control" name="QtdAdolescentes" value="">
  </div>
  <div class="form-group">
      <label>Valor ppor adolescente:</label>
      <input type="text" name="ValorAdolescente" class="form-control" id="valorAdolescente" maxlength="12">
  </div>
  <div class="form-group">
    <label>Valor Total:</label>
    <input type="text" id="valorTotal" class="form-control" name="ValorTotal" value="">
  </div>
  <div class="form-group">
      <label>Observações:</label>
      <textarea name="Observacoes" class="form-control" style="height: 230px"></textarea>
  </div>
  <div class="form-group text-center">
    <button type="submit" name="Submit" value="Avancar" class="btn btn-success">Cadastrar e enviar link por e-mail</button>
  </div>
</form>
  • Could put the html code?

  • 1

    You’re right Felipe. I forgot to put the code. I changed my post. The value fields have mask.

1 answer

1

Try something like:

SE6+:

let arrTipos = ['Adulto', 'Criancas6Anos', 'Criancas6a12Anos', 'Adolescentes'];
let total = 0;
for (var tipo of arrTipos) {
    if($('[name=Qtd'+ tipo +']').val() && $('#valor'+ tipo).val()){
        total += (parseInt($('[name=Qtd'+ tipo +']').val())*parseFloat($('#valor'+ tipo).val()));
    }
}
$('#valorTotal').val(total);

SE5:

var arrTipos = ['Adulto', 'Criancas6Anos', 'Criancas6a12Anos', 'Adolescentes'];
var total = 0;
for (var index in arrTipos) {
    var tipo = arrTipos[index];
    if($('[name=Qtd'+ tipo +']').val() && $('#valor'+ tipo).val()){
        total += (parseInt($('[name=Qtd'+ tipo +']').val())*parseFloat($('#valor'+ tipo).val()));
    }
}
$('#valorTotal').val(total);
  • 1

    Just to complement you will need to import Jquery into your HTML.

  • Hello Felipe. I tried with the 02 examples, but they didn’t work. The Total value field remains 0. I changed the mask of the fields $("#valorAdulto").maskMoney({symbol:'R$ ', thousands:'.', decimal:'.', symbolStay: true}); for $("#valorAdulto").maskMoney({thousands:'', decimal:'.', symbolStay: true}); and yet it didn’t work.

  • @Fox.11 What is the value of the field? R$1,000.00 or 1000.00?

  • The default is R$ 1,000.00. I switched to 1000.00 to see if it worked. But it can be one or the other.

  • @Fox.11 he’s taking the amount and the amount?

Browser other questions tagged

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