JavaScript数学没有正确加法

时间:2015-05-08 16:12:29

标签: javascript jquery html

我的数学函数问题如下。 depreciationFee变量正确加起来,但由于某些奇怪的原因,financeFee变量没有。我正在尝试计算车辆的每月租赁费用。每当我提交financeFee的数字时,它会显示两个相互附加的数字而不是相加。有没有理由这些数字没有正确加起来?

$(".submit").click(function() {

  function calculateLease() {

    var capitalCost = $(".capital-cost").val();
    var downPayment = $(".down-payment").val();
    var residualCost = $(".residual-cost").val();
    var monthTerm = $(".month-term").val();
    var moneyFactor = $(".money-factor").val();

    var depreciationFee = (((capitalCost - downPayment) - residualCost) / monthTerm);

    // THIS IS THE ONE THAT DOESN'T WORK
    var financeFee = ((capitalCost - downPayment) + residualCost);

    alert(financeFee);

  }

  calculateLease();

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="lease-calculator-container">
  <h3>LEASE CALCULATOR</h3>
  <form method="get">
    <input type="text" class="capital-cost" placeholder="MSRP" />
    <br />
    <input type="text" class="down-payment" placeholder="DOWN PAYMENT" />
    <br />
    <input type="text" class="residual-cost" placeholder="RESIDUAL" />
    <br />
    <input type="text" class="month-term" placeholder="TERM IN MONTHS" />
    <br />
    <input type="text" class="money-factor" placeholder="MONEY FACTOR" />
    <br />
  </form>
  <input type="submit" class="submit" value="CALCULATE" />
  <div class="monthly-cost"></div>
  <div class="total-cost"></div>
</div>

1 个答案:

答案 0 :(得分:0)

为整数做parseInt(value,10)或为浮动做parseFloat(value)

如果数据类型不是数字,则JavaScript会附加值。