Take select values and add their data

Asked

Viewed 443 times

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 guys.

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

That 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>

It is possible to do this with Jquery?

  • will always come apart by / and that select it’s kinda weird he’s <select></select>?

  • 1

    @Virgilionovic made the correction, was missing the tag select and added to run html. See if you can understand now.

  • now you’ve changed the question? Is that really how it is to come? One is with equal to another one R$, please put correctly as is the true way?

  • Hi @Virgilio I changed right when you answered me. Sorry. But this last modification is my problem. Thanks

  • I put an example with = see if it’s right for you? has a pattern look like it would be!

  • Just the way I need it. Thank you very much.

Show 1 more comment

1 answer

2


If there is such a pattern it can be done so:

$('#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>

<select id="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" />

Browser other questions tagged

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