javascript计算答案问题

时间:2016-03-29 03:27:30

标签: javascript html

亲爱的朋友们,我在js计算中有一些简单的错误。添加任意数字后显示其工作正常(显示计算)。但我添加0或空txtfiled计算答案不重置。

我可以知道这是什么问题吗? P

 <html>
 <head> 
 <style>
    #demo1{
      font-size:30px;
      color:green;
    }
    </style>
 </head> 

<body>



<h1>GRAND TOTAL (LKR)</h1>

ENTER TARGET WEIGHT  <input type="text" name="val1" id="val1" oninput="cal ()" /> 
<input type="button" value="submit"  />

<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<p id="demo4"></p>
<p id="demo5"></p>
<p id="demo6"></p>
<p id="demo7"></p>
<p id="demo8"></p>
<p id="demo9"></p>



<script type="text/javascript"> 

function cal() { 

     var inputValue = Number(document.getElementById('val1').value);

 if ((inputValue=>1)&&(inputValue<=16))
{
var  fsc = 0.88*inputValue;
var  calculatedAmount = 71.25+fsc+90+55+10;
var  con = calculatedAmount * 106;
var  tot = con+11500;



 document.getElementById("demo1").innerHTML =("WEIGHT BASE="+" "+(inputValue)+"KG"+"");
 document.getElementById("demo2").innerHTML =("FREIGHT ="+" "+"71.5"+""+"<br>");
 document.getElementById("demo3").innerHTML =("FSC/SSC ="+" "+(fsc.toFixed(2))+""+"<br>");
 document.getElementById("demo4").innerHTML =("HANDLING ="+" "+"90"+""+"<br>");
 document.getElementById("demo5").innerHTML =("EXW ="+" "+"55"+""+"<br>");
 document.getElementById("demo6").innerHTML =("SCREENING ="+" "+"10"+""+"<br><br>");

document.getElementById("demo7").innerHTML =("ORIGIN TOTAL (SGD)="+" "+(calculatedAmount.toFixed(2))+""+"<br>");
document.getElementById("demo8").innerHTML =("ORIGIN CHARGES TOTAL (LKR)="+" "+con.toFixed(2)+"<br>");
document.getElementById("demo9").innerHTML =("GRAND TOTAL (LKR)="+" "+tot.toFixed(2));

}
else if((inputValue>16)&&(inputValue<=45))
{
var  fre = 4.3*inputValue;
var  fsc = 0.88*inputValue;
var  calculatedAmount = fre+fsc+90+55+10;
var  con = calculatedAmount * 106;
var  tot = con+11500;


 document.getElementById("demo1").innerHTML =("WEIGHT BASE="+" "+(inputValue)+"KG"+"");
 document.getElementById("demo2").innerHTML =("FREIGHT ="+" "+(fre.toFixed(1))+""+"<br>");
 document.getElementById("demo3").innerHTML =("FSC/SSC ="+" "+(fsc.toFixed(1))+""+"<br>");
 document.getElementById("demo4").innerHTML =("HANDLING ="+" "+"90"+""+"<br>");
 document.getElementById("demo5").innerHTML =("EXW ="+" "+"55"+""+"<br>");
 document.getElementById("demo6").innerHTML =("SCREENING ="+" "+"10"+""+"<br><br>");

document.getElementById("demo7").innerHTML =("ORIGIN TOTAL (SGD)="+" "+(calculatedAmount.toFixed(2))+""+"<br>");
document.getElementById("demo8").innerHTML =("ORIGIN CHARGES TOTAL (LKR)="+" "+con.toFixed(2)+"<br>");
document.getElementById("demo9").innerHTML =("GRAND TOTAL (LKR)="+" "+tot.toFixed(2));
}
else if((inputValue>45)&&(inputValue<100))
{
var  fre = 2.1*inputValue;
var  fsc = 0.88*inputValue;
var  calculatedAmount = fre+fsc+90+55+10;
var  con = calculatedAmount * 106;
var  tot = con+11500;



document.getElementById("demo1").innerHTML =("WEIGHT BASE="+" "+(inputValue)+"KG"+"");
document.getElementById("demo2").innerHTML =("FREIGHT ="+" "+(fre.toFixed(1))+""+"<br>");
document.getElementById("demo3").innerHTML =("FSC/SSC ="+" "+(fsc.toFixed(1))+""+"<br>");
document.getElementById("demo4").innerHTML =("HANDLING ="+" "+"90"+""+"<br>");
document.getElementById("demo5").innerHTML =("EXW ="+" "+"55"+""+"<br>");
document.getElementById("demo6").innerHTML =("SCREENING ="+" "+"10"+""+"<br><br>");

document.getElementById("demo7").innerHTML =("ORIGIN TOTAL (SGD)="+" "+(calculatedAmount.toFixed(2))+""+"<br>");
document.getElementById("demo8").innerHTML =("ORIGIN CHARGES TOTAL (LKR)="+" "+con.toFixed(2)+"<br>");
document.getElementById("demo9").innerHTML =("GRAND TOTAL (LKR)="+" "+tot.toFixed(2));
}
else if((inputValue>100)&&(inputValue<300))
{
var  fre = 340;
var  fsc = 0.88*inputValue;
var  calculatedAmount = fre+fsc+90+55+10+45;
var  con = calculatedAmount * 106;
var  tot = con+11500;



document.getElementById("demo1").innerHTML =("WEIGHT BASE="+" "+(inputValue)+"KG"+"");
document.getElementById("demo2").innerHTML =("FREIGHT ="+" "+(fre.toFixed(1))+""+"<br>");
document.getElementById("demo3").innerHTML =("FSC/SSC ="+" "+(fsc.toFixed(1))+""+"<br>");
document.getElementById("demo4").innerHTML =("HANDLING ="+" "+"90"+""+"<br>");
document.getElementById("demo5").innerHTML =("EXW ="+" "+"55"+""+"<br>");
document.getElementById("demo6").innerHTML =("SCREENING ="+" "+"10"+""+"<br><br>");

document.getElementById("demo7").innerHTML =("ORIGIN TOTAL (SGD)="+" "+(calculatedAmount.toFixed(2))+""+"<br>");
document.getElementById("demo8").innerHTML =("ORIGIN CHARGES TOTAL (LKR)="+" "+con.toFixed(2)+"<br>");
document.getElementById("demo9").innerHTML =("GRAND TOTAL (LKR)="+" "+tot.toFixed(2));
}
else if((inputValue>300)&&(inputValue<500))
{
var  fre = 1.9*inputValue;
var  fsc = 0.88*inputValue;
var  han = 0.6*inputValue;
var  exw = 0.25*inputValue;
var  calculatedAmount = fre+fsc+han+exw+10+45;
var  con = calculatedAmount * 106;
var  tot = con+12250;



document.getElementById("demo1").innerHTML =("WEIGHT BASE="+" "+(inputValue)+"KG"+"");
document.getElementById("demo2").innerHTML =("FREIGHT ="+" "+(fre.toFixed(1))+""+"<br>");
document.getElementById("demo3").innerHTML =("FSC/SSC ="+" "+(fsc.toFixed(1))+""+"<br>");
document.getElementById("demo4").innerHTML =("HANDLING ="+" "+(han.toFixed(1))+""+"<br>");
document.getElementById("demo5").innerHTML =("EXW ="+" "+(exw.toFixed(1))+""+"<br>");
document.getElementById("demo6").innerHTML =("SCREENING ="+" "+"10"+""+"<br><br>");

document.getElementById("demo7").innerHTML =("ORIGIN TOTAL (SGD)="+" "+(calculatedAmount.toFixed(2))+""+"<br>");
document.getElementById("demo8").innerHTML =("ORIGIN CHARGES TOTAL (LKR)="+" "+con.toFixed(2)+"<br>");
document.getElementById("demo9").innerHTML =("GRAND TOTAL (LKR)="+" "+tot.toFixed(2));
}
else if((inputValue>500)&&(inputValue<1000))
{

var  fre = 1.6*inputValue;
var  fsc = 0.88*inputValue;
var  han = 0.6*inputValue;
var  exw = 0.25*inputValue;
var  calculatedAmount = fre+fsc+han+exw+10+45;
var  con = calculatedAmount * 106;
var  tot = con+16250;


document.getElementById("demo1").innerHTML =("WEIGHT BASE="+" "+(inputValue)+"KG"+"");
document.getElementById("demo2").innerHTML =("FREIGHT ="+" "+(fre.toFixed(1))+""+"<br>");
document.getElementById("demo3").innerHTML =("FSC/SSC ="+" "+(fsc.toFixed(1))+""+"<br>");
document.getElementById("demo4").innerHTML =("HANDLING ="+" "+(han.toFixed(1))+""+"<br>");
document.getElementById("demo5").innerHTML =("EXW ="+" "+(exw.toFixed(1))+""+"<br>");
document.getElementById("demo6").innerHTML =("SCREENING ="+" "+"10"+""+"<br><br>");

document.getElementById("demo7").innerHTML =("ORIGIN TOTAL (SGD)="+" "+(calculatedAmount.toFixed(2))+""+"<br>");
document.getElementById("demo8").innerHTML =("ORIGIN CHARGES TOTAL (LKR)="+" "+con.toFixed(2)+"<br>");
document.getElementById("demo9").innerHTML =("GRAND TOTAL (LKR)="+" "+tot.toFixed(2));
}
else if(inputValue>1000)
{

var  fre = 1.5*inputValue;
var  fsc = 0.88*inputValue;
var  han = 0.6*inputValue;
var  exw = 0.25*inputValue;
var  calculatedAmount = fre+fsc+han+exw+11+45;
var  con = calculatedAmount * 106;
var  tot = con+16250;



 document.getElementById("demo1").innerHTML =("WEIGHT BASE="+" "+(inputValue)+"KG"+"");
document.getElementById("demo2").innerHTML =("FREIGHT ="+" "+(fre.toFixed(1))+""+"<br>");
document.getElementById("demo3").innerHTML =("FSC/SSC ="+" "+(fsc.toFixed(1))+""+"<br>");
document.getElementById("demo4").innerHTML =("HANDLING ="+" "+(han.toFixed(1))+""+"<br>");
document.getElementById("demo5").innerHTML =("EXW ="+" "+(exw.toFixed(1))+""+"<br>");
document.getElementById("demo6").innerHTML =("SCREENING ="+" "+"10"+""+"<br><br>");

document.getElementById("demo7").innerHTML =("ORIGIN TOTAL (SGD)="+" "+(calculatedAmount.toFixed(2))+""+"<br>");
document.getElementById("demo8").innerHTML =("ORIGIN CHARGES TOTAL (LKR)="+" "+con.toFixed(2)+"<br>");
document.getElementById("demo9").innerHTML =("GRAND TOTAL (LKR)="+" "+tot.toFixed(2));
}


else
{

alert("Error Please Check INPUT");

}



} 


</script>

</body> 

</html>

2 个答案:

答案 0 :(得分:1)

var calculatedAmount = 71.5 + 0.88 + 90 + 55 + 10; &lt; -71.5不是71.25,它只是一个简单的错字:)

答案 1 :(得分:0)

  

var calculatedAmount = 71.5 + 0.88 + 90 + 55 + 10;

你已经把71.5而不是71.25。