Search code examples
jqueryformsjquery-calculation

jQuery get text as number and print result


My code doesn't work

    function compute() {
    if ( $('option[name=oage]:checked').val() != undefined ) {
        var a = $('input[name=odeliv]').val();
        var b = $('select[name=oage]').val();
        var c = $('select[name=otime]').val();

        var total = (b * c) + a;
        $('#result').val(total);
    }
}

$('select[name=oage]').change(compute);
$('input[name=odeliv]').change(compute);
$('select[name=otime]').change(compute);

need calculate summary price for service (var total)

Fiddle


Solution

  • Try This code Changes have been done

    function compute() {
        if ( $('option[name=oage]:checked').val() != undefined ) {
            var a = parseInt($("select[name='oage'] option:selected").val());
            var b = parseInt($("input[name*='odeliv']:checked").val());
            var c = parseInt($("select[name='otime'] option:selected").val());
            var total = (b * c) + a;
    
            $('#result').val(total);
        }
    }
    
    $('select[name=oage]').change(compute);
    $('input[name=odeliv]').change(compute);
    $('select[name=otime]').change(compute)
    compute();
    <form method="POST" action="http://strahovka.cz/secure/payment.php" id="preprice">
      <div class="vc_col-sm-4"><label for="otime"><h3>Срок страхования</h3></label>
        <select name="otime">
          <option name="otime" id="otime" value="1">3 месяцев</option>
          <option name="otime" id="otime" value="2">6 месяцев</option>
          <option name="otime" id="otime" value="4">12 месяцев</option>
          <option name="otime" id="otime" value="8">24 месяца</option>
        </select>
      </div>
      <div class="vc_col-sm-3"><label for="oage"><h3>Возраст</h3></label>
        <select name="oage">
          <option name="oage" id="oage" value="3600">0-5 лет</option>
          <option name="oage" id="oage" value="1600">6-14 лет</option>
          <option name="oage" id="oage" value="1200">15-30 лет (студенты)</option>
          <option name="oage" id="oage" value="1600">31-60 лет</option>
        </select>
      </div>
      <div class="vc_col-sm-3"><label for="odeliv"><h3>Тип доставки</h3></label>
        <input name="odeliv" id="odeliv" value="0" type="radio" checked> Бесплатно в Праге<br/>
        <input name="odeliv" id="odeliv" value="0" type="radio"> Забарать в офисе<br/>
        <input name="odeliv" id="odeliv" value="55" type="radio"> Заказное письмо по Чехии<br/>
        <input name="odeliv" id="odeliv" value="155" type="radio"> Заказное международное письмо<br/>
      </div>
      <div class="vc_col-sm-4"><label for="qoupon"><h3>Итоговая стоимость</h3></label>
        <input value="0" id="result" name="result" type="text" disabled="disabled" />
      </div>
    
      <div class="vc_col-sm-10">
        <div class="vc_col-sm-5" style="text-align: center;"><div class="row"><input value="Оформить страховку на себя" type="submit" /></div></div>
        <div class="vc_col-sm-5" style="text-align: center;"><div class="row"><input value="Оформить страховку на друга" type="submit" /></div></div>
      </div>
    </form>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>