Multiply and Add according to Dropdown

Asked

Viewed 139 times

0

In this code below it makes the calculation according to the checkbox but I do not know how to make the third field work. This third field only has to multiply a value by the number chosen in the dropdown, moving as little as possible in this already existing code

  • Example $Value = 50; $value x number in dropdown and sum in total value

$(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);
        $('#total1').val(total.toFixed(2));        
    }); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

<span class="opensans size13"><b>500,00 Ida e Volta?  
<input type="checkbox" class="valores1" name="choice" value="500" /><br/>

<span class="opensans size13"><b>200,00 Adcional?  
<input type="checkbox" class="valores1" name="choice" value="200" /><br/>
                                    
<span class="opensans size13"><b>50,00 Cadeira  X     
<select  class="valores1" name="cadeira" >
	<option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
</select>  //Fazer esse função funcionar
<br/>
                                    <br />                                    

<input type="text" size="6"  readonly="" name="total" id="total1" value="1000" /><br /><br />

1 answer

1


Take the value of the item selected by the select id.

The estate ..selectedIndex].value - gets the value of the currently selected item

var e = document.getElementById("valores2"); var itemSelecionado = e.options[e.selectedIndex].value;

$(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;
    total=total+(itemSelecionado*50);
    $('#total1').val(total.toFixed(2));        
  });  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<span class="opensans size13"><b>500,00 Ida e Volta?  
<input type="checkbox" class="valores1" name="choice" value="500" /><br/>

<span class="opensans size13"><b>200,00 Adcional?  
<input type="checkbox" class="valores1" name="choice" value="200" /><br/>
                                    
<span class="opensans size13"><b>50,00 Cadeira  X     
<select id="valores2"  class="valores1" name="cadeira" >
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
</select>  //Fazer esse função funcionar
<br/>
                                    <br />                                    

<input type="text" size="6"  readonly="" name="total" id="total1" value="1000" /><br /><br />

Browser other questions tagged

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