Get select values and add their data

1

<div class="row">
     <div class="form-group col-md-3">
       <label for="servico">Serviço</label>
       <select>
    <option value="200">Escapamento / Montagem R$ 100</option>                             </select>

      </div>
<div class="form-group col-md-4">
 <label for="valorTotal">Total</label>                                 
 <input type="text" class="form-control" id="valorTotal" name="valorTotal" value="" readonly >
  </div>
 </div>

Good morning people.

I have a select that displays the service name, part value, and assembly. I wanted that when selecting the service, 200 + 100 should be added and presented in another input.

This would be my select:

 <div class="row">
     <div class="form-group col-md-3">
       <label for="servico">Serviço</label>
    <select>
<option value="200">Escapamento / Montagem = 100</option>                             </select>

  </div>          </div>
<div class="form-group col-md-4">
 <label for="valorTotal">Total</label>                                 
 <input type="text" class="form-control" id="valorTotal" name="valorTotal" value="" readonly >
  </div>
 </div>

Can I do this with JQuery?

    
asked by anonymous 06.02.2017 / 14:13

1 answer

2

If this pattern exists it can be done like this:

$('#select1').on('change', function(e) {
  var n1 = parseInt($(this).val());  
  var n2 = parseInt($(this).text().split("=")[1].trim());
  $('#result').val(n1 + n2);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><selectid="select1">
  <option></option>
  <option value="100">Escapamento / Montagem = 100</option>
  <option value="200">Escapamento / Montagem = 200</option>
  <option value="300">Escapamento / Montagem = 300</option>
<select>

<input type="text" readonly id="result" />
    
06.02.2017 / 14:21