Zerar Specific field in the Form

Asked

Viewed 54 times

0

As I do not have much knowledge in javascript I ask a help from you... In my code below I need to reset a specific field that is the field People by the last Select field

This form is a calculator, I wish if by clicking reset in the last field it retains only the field People automatically correcting the somatorio

I need a simple solution without changing this code completely

$(document).ready(function() {
  $(".valores1").change(function() {
    var total = 1000;
    total += $('input[class="valores1"]:checked').get().reduce(function(tot, el) {
      return tot + Number(el.value);
    }, 0);
        
        
    var e = document.getElementById("valores2");
    var itemSelecionado = e.options[e.selectedIndex].value;
    
    var primeiroDigito = (itemSelecionado.substring(0,1));
    total=total+(primeiroDigito*100);
    
    
    
    var d = document.getElementById("valores3");
    var itemSelecionado3 = d.options[d.selectedIndex].value;
    
    var primeiroDigito3 = (itemSelecionado3.substring(0,1));
    total=total+(primeiroDigito3*100);
    
    var g = document.getElementById("valores4");
    var itemSelecionado4 = g.options[g.selectedIndex].value;
    
        
    var primeiroDigito4 = (itemSelecionado4.substring(0,1));
    total=total+(primeiroDigito4/100);
    
    
    
    
    
    
    //aqui pega primeiro digito
    
    $('#total1').val(total.toFixed(2));     
  });  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Duplica:<input type="checkbox" class="valores1" onclick="Mudarestado('minhaDiv')" name="direcao" value="1000" id="option_1"  />
Adciona:<input type="checkbox" class="valores1" name="bilingue" value="200" id="option_1"  />

<select id="valores2"   class="valores1 form-control" name="cadeirinha" >
    <option value="0">Passageiros</option>
    <option value="1">x1</option>
    <option value="2">x2</option>
    <option value="3">x3</option>
    
</select>  

<select id="valores3"   class="valores1 form-control" name="cadeirinha2" >
    <option value="0">Pessoas</option>
    <option value="1">x1</option>
    <option value="2">x2</option>
    <option value="3">x3</option>
    
</select>  

<select id="valores4"   class="valores1 form-control" name="cadeirinha2" >
    
    <option value="0"></option>
    <option value="0">zerar</option>
    <option value="2">x2</option>
   
    
</select>  


<input type="text" size="5"  readonly="" name="valor" id="total1" value="1000.00" style="background-color: transparent; border-color: transparent; font-weight: bold; font-size: 18px; color: green; "    />

1 answer

1


First of all, there’s only one small problem in <option value="0">zerar</option>, who is with the same value of default. For the code I added below, I took into account that this option was changed to 1.

If the reset option is selected, I reset the people field and decrease the total from the value of the people field.

if (g.value == 1) {
  d.value = 0;
  g.value = 0;
  total = total-(primeiroDigito3*100);
  $('#total1').val(total);
}

Browser other questions tagged

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