S
S
spec20072018-03-20 12:25:56
JavaScript
spec2007, 2018-03-20 12:25:56

How to fix js error?

Tell me where the error is dear ......
It considers the text and not the value ... It is necessary 100 + 1 = 101 and it displays the total: 1001

<div class="calc">
<label>Марка автомобиля</label>
<select class="form-control" name="card" id="card">
<option value="">Выбрать</option>
<option value="100">MERSEDES</option>
<option value="200">KIA</option>
<option value="300">LADA</option>
</select>
<br>
<label>Тип автомобиля</label>
<select name="paper" id="paper">
<option value="">Выбрать</option>
<option value="1">Универсал</option>
<option value="2">Купе</option>
</select> 
<br>
<span id="price"></span> руб.
</div>

$(function() {
  $('.calc').change(function(){
    $card = $('select#card').val();
    $paper = $('select#paper').val();
    $('span#price').text($card+$paper);
  });
});

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dymok, 2018-03-20
@spec2007

$(function() {
  $('.calc').change(function(){
    $card = parseInt($('select#card').val());
    $paper = parseInt($('select#paper').val());
    $('span#price').text($card+$paper);
  });
});

I
imdeveloper, 2018-03-20
@link_web

You need to convert strings to numbers and everything will work correctly
Like this: https://jsfiddle.net/o2gxgz9r/36567/

S
spec2007, 2018-03-20
@spec2007

Thanks everyone, it helped.
And you can somehow use here not only select, but fasten the checkbox ....
for example like this:

<label><input name="element1" id="element1" type="checkbox" value="3000" /> 2-х дверей</label>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question