提问者:小点点

JavaScript/jQuery中的节省计算


我想知道是否有人可以帮助我用javascript或jQuery进行计算?成本的百分比必须定为35%。平均节省额必须固定在19%。例如:1百万欧元x0.35x0.19=66.500欧元的节余

我在这里创建了这段代码,但我被计算困住了

<div id="calculator" class="calculator">
  <label class="title">Savings Calulator : Basic Compound Interest</label>
  <hr/>
  <br/>
  <label>Input ($)</label>
  <input type="number" id="principle" value="0" min="0">
    <br/>
  <label>Interest Rate (%)</label>
  <input type="number" id="interest-rate" value="0.35" min="0">
  </br>
    <label>Interest Rate (%)</label>
  <input type="number" id="interest-rate-two" value="0.19" min="0">
    <br/>
  <hr/>
  <div id="calc-result" class="result">
    $0
  </div>
</div>

https://codepen.io/john_098/pen/bapvqjp

谢谢


共1个答案

匿名用户

在javascript&jQuery中,您可以使用*来乘数,使用/来除数。

这是一个简单的脚本,用于计算3个输入值,并将结果输出到result div中

null

$("#calc").on("click", function(){
  let principle = $("#principle").val();
  let interest = $("#interest-rate").val();
  let interest2 = $("#interest-rate-two").val();
  let calc = principle / 100 * interest / 100 * interest2;
  // divide principle by 100% to get 1%, multiply by interest(35%). 
  // divide that to get 1% of the total after the first interest rate
  // multiply by interest rate 2 to get the final calculation
  $(".result").text(calc);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="calculator" class="calculator">
  <label class="title">Savings Calulator : Basic Compound Interest</label>
  <hr/>
  <br/>
  <label>Input ($)</label>
  <input type="number" id="principle" value="0" min="0">
    <br/>
  <label>Interest Rate (%)</label>
  <input type="number" id="interest-rate" value="35" min="0">
  </br>
    <label>Interest Rate (%)</label>
  <input type="number" id="interest-rate-two" value="19" min="0">
    <br/>
  <hr/>
  <div id="calc">calculate</div>
  <div id="calc-result" class="result">
    
  </div>
</div>