这个javascript代码工作不正常(if-else语句).it有输出但是当我输入term = 35时,总是计算term = 36 shows.someone可以帮我解决这个问题吗?谢谢!我只是编程的新手。
<script language="javascript" type="text/javascript">
function calc1(form) {
var principal = form.principal.value;
var term = form.term.value;
var paidup = form.paidup.value;
var unearn = form.unearn.value;
form.paidup.value = form.principal.value * 0.01;
if (principal >= 50000, term = 36) {
form.unearn.value = principal * .27750;
}
else if (principal >= 50000, term = 35) {
form.unearn.value = principal * .27000;
}
}
</script>
<FORM>
<table>
<tr>
<td>Enter Principal Amount:</td>
<td><INPUT TYPE ="text" NAME="principal" SIZE=15 ></td>
</tr>
<tr>
<td>Enter Number of Term:</td>
<td><INPUT TYPE ="text" NAME="term" SIZE=15 ></td>
</tr>
<tr> <td><INPUT TYPE ="button" VALUE="Calculate" ONCLICK="calc1(this.form)"></td><td></td> </tr>
</tr> <tr> <td>Paid-Up Share Capital:</td> <td><INPUT TYPE="text" NAME="paidup" SIZE=4 ></td> </tr> <tr> <td>Unearned Income:</td> <td><INPUT TYPE="text" NAME="unearn" SIZE=4 ></td> </tr>
</table>
</form>
答案 0 :(得分:0)
您在if
中使用了逗号运算符。这会将term
设置为36。
if (principal>=50000, term=36)
我不确定你想要达到的目标,但也许这会更好:
var term = parseInt(form.term.value, 10),
principal = parseInt(form.principal.value, 10);
if (principal >= 50000 && term == 36) {...} // (if principal is greater than or equal to 5000 and term equals to 36)
答案 1 :(得分:0)
你的所有代码都有一些问题,我会解决所有问题,因为你说你是新手,所以我希望这会有所帮助:)
你总是获得36的原因是因为在你的if语句中你有term = 36(一个相等)所以你将term设置为36。
在if语句中使用principal> = 50000,term = 36是没有意义的,因为忽略了第一个表达式,我认为你想做的是
if(principal&gt; = 50000&amp;&amp; term === 36){
注意我使用===而不是==了解更多信息,请点击此处:Which equals operator (== vs ===) should be used in JavaScript comparisons?
不要使用内联处理程序(在html onclick =“某些功能”内)请阅读http://www.quirksmode.org/js/introevents.html