计算2个期权价值之和

时间:2019-01-27 16:02:43

标签: javascript jquery ajax

我想添加2个选择选项的值,然后输出结果。

到目前为止,我尝试过什么,但是只返回值1。我想要的是将n_adult和n_children的值相加,然后将结果输出到 人数:

function familyRoom() {
  if ($('select#selectBoxFamily option').length > 1) {
    var person = Number($('#n_adult').val()) + Number($('#n_children').val());

    $('#selectBoxFamily').find("option:nth-last-child(-n+" + $('#selectBoxFamily').val() + ")").remove();
    family++;
    if (family <= 1) {
      $("#roomDetail ul").append('<li><strong>Family Room - Regular Online Rate</strong> </li>')
                         .append('<li class="pull-right"><h4 style="color:darkorange">PHP {{$availableRooms[2][\' nightRate \']}}</h4></li>')
                         .append('<li>Number of night(s): {{$n_nights}} </li>')
                         .append('<li>Number of person(s):</li>')
                         .append(person)
                         .append('<li class="hr">Number of room(s): </li>');
    }
  } else {
    alert("No more rooms");
  }
}
<div class="pull-left col-xs-12 col-sm-4 col-md-4"><label for="adult" style="color:black">No. of Adult: </label>
  <select required tabindex="9" id="n_adult" name="n_adult">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
  </select>
</div>
<div class="pull-left col-xs-12 col-sm-4 col-md-4">
  <label for="children" style="color:black">No. of Children: </label>
  <select required tabindex="10" id="n_children" name="n_children">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
  </select>
</div>

1 个答案:

答案 0 :(得分:1)

您可以简单地

Number($("#n_adult").val()) + Number($("#n_children ").val())

$("#cal").click(function(){

 console.log(Number($("#n_adult").val()) + Number($("#n_children").val()))
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<div class="pull-left col-xs-12 col-sm-4 col-md-4"><label for="adult" style="color:black">No. of Adult: </label>
  <select required tabindex="9" id="n_adult" name="n_adult">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
  </select>
</div>
<div class="pull-left col-xs-12 col-sm-4 col-md-4">
  <label for="children" style="color:black">No. of Children: </label>
  <select required tabindex="10" id="n_children" name="n_children">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
  </select>
  
  <button id="cal">Calculate</button>
</div>