Jquery加法和乘法计算

时间:2017-07-06 20:01:57

标签: jquery calculation

我正在计算一些Jquery变量,但不知道为什么如果条件没有变为真,请查看以下代码

function roomCalc(){
  var noOfAdults = 2; // this is dynamic //parseInt($('#adults').val());
  var noOfChilds = 0;// this is dynamic parseInt($('#childs').val());
  var noofXtraAdults = 15000;//parseInt($('#extraAdult').val());
  var noofXtraChilds = 15000;//parseInt($('#extraChild').val());
  var maxRoomToatal = 1000;//parseInt($('#maxRoomToatal').text());

  $('#adults').keyup(function(){
    if(noOfAdults <= 2 )
  {
      var totalOutput = maxRoomToatal;
  }
  else if(noOfAdults >= 3 ){
    var totalOutput = noofXtraAdults + maxRoomToatal;
  }

  alert(totalOutput); 
});

}
roomCalc();

1 个答案:

答案 0 :(得分:3)

您尚未将输入值分配给noOfAdults。

&#13;
&#13;
function roomCalc() {
  var noOfAdults = 2; // this is dynamic //parseInt($('#adults').val());
  var noOfChilds = 0; // this is dynamic parseInt($('#childs').val());
  var noofXtraAdults = 15000; //parseInt($('#extraAdult').val());
  var noofXtraChilds = 15000; //parseInt($('#extraChild').val());
  var maxRoomToatal = 1000; //parseInt($('#maxRoomToatal').text());

  $('#adults').keyup(function() {
    noOfAdults = $('#adults').val() > 0 ? $('#adults').val() : noOfAdults;
    if (noOfAdults <= 2) {
      var totalOutput = maxRoomToatal;
    } else if (noOfAdults >= 3) {
      var totalOutput = noofXtraAdults + maxRoomToatal;
    }

    $("#maxRoomToatal").html(totalOutput);

  });

}
roomCalc();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="adults" type='number'>
<div id="maxRoomToatal"></div>
&#13;
&#13;
&#13;