具有许多变量和if语句的函数不起作用

时间:2018-07-20 12:42:52

标签: javascript function calculator

我正在设计一个网络计算器,现在有四个input type="number",每个都有一个值。

我有rangespan(在其中显示值)。 sumUpNewCar函数对所有input值求和,并在id="showPr"中显示该值。

var slider = document.getElementById("pr");
var output = document.getElementById("showPr");
output.innerHTML = slider.value;

slider.oninput = function() {
  output.innerHTML = this.value + "%";
}

function sumUpNewCar() {
  var promoLoan = document.getElementById("newAuto1").value;
  var standardLoan = document.getElementById("newAuto2").value;
  var promoPlusLoan = document.getElementById("newAuto3").value;
  var standardPlusLoan = document.getElementById("newAuto4").value;
  var penetrationLoan = document.getElementById("showPr"); /* doesn't work */
  var sumPromoLoans = document.getElementById("sumPromoLoans").innerHTML = (promoLoan * Number(0.004)) + (promoPlusLoan * Number(0.0004));
  var sumStandardLoans = document.getElementById("sumStandardLoans").innerHTML = (standardLoan * 0.07) + Math.round(standardPlusLoan * 0.028);
  var sumNewVehicle = document.getElementById("sumNewVehicle").innerHTML = sumPromoLoans + sumStandardLoans;
  var overallPromo = sumPromoLoans.value;

  /* doesnt work*/
  if (penetrationLoan > 24.99 && penetrationLoan < 35) {
    overallPromo *= 0.005;
  }
}

/*
else if(penetrationLoan >= 35){
    sumPromoLoans * Number(0.01);
    sumStandardLoans * Number(0.02);
}
else if(penetrationLoan >=25 <=29.99){
    sumStandardLoans * Number(0.01);
 }
 else if(penetrationLoan >=30 <=34.99){
    sumStandardLoans * Number(0.015);
 } else {
    sumNewVehicle * Number(1);
 }

}
*/
<div class="mainbox">
  <form>
    <input class="promo" type="number" min="1000" max="100000000" id="newAuto1" onchange="sumUpNewCar()">
    <input class="standard" type="number" min="100000" max="100000000" id="newAuto2" onchange="sumUpNewCar()">
    <input class="promoPlus" type="number" min="100000" max="100000000" id="newAuto3" onchange="sumUpNewCar()">
    <input class="standardPlus" type="number" min="100000" max="100000000" id="newAuto4" onchange="sumUpNewCar()">
  </form>
</div>
<div class="mainbox2">
  <div class="newvehicle2"><b>bonus</b></div>
  <input type="range" min="20" max="40" step="1" class="slider" value="20" id="pr">
  <div class="showPr"><span class="showPr2" id="showPr" onchange="sumUpNewCar()"> </span> </div>
  <div class="mainbox6">
    <div class="overall"></div>
    <table class="tableOverall">
      <tr>
        <td id="sumNewVehicle"></td>
        <td id="sumPromoLoans"></td>
        <td id="sumStandardLoans"></td>
      </tr>
      <tr>
        <td id="sumUsedVehicle"></td>
      </tr>
      <tr>
        <td id="sumSpr"></td>
      </tr>
      <tr>
        <td id="sumPrs"></td>
      </tr>
      <tr>
        <td id="sumUp"></td>
      </tr>
    </table>
  </div>

最后,我更改为:

penetrationLoan=document.getElementById("pr").value

并放入:

<input type="range" min="20" max="40" step="1" class="slider" value="20" id="pr" onchange="sumUpNewCar()">

它仍然没有用。

0 个答案:

没有答案