感谢您的帮助!除了数学,我还有几个问题。Javascript计算器未回复的问题
1)当一个字段没有填写,我希望它抛出一个警告,并把重点重新尚未填写最上面的输入字段。例如)4月份被填写,但不是贷款期限,所以重点应该返回贷款期限文本框。
2)我不想每月支付显示,直至所有的文本字段都填写。
我试图玩弄它,但它仍然没有工作:(
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- This is assign05.html -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> JavaScriptcript Mortgage Calculator </title>
<script>
//Display the total sum
function display(x, y, z) {
var num = x + y + z;
var n = num.toString();
document.getElementById("total").innerHTML = "Your monthly payment is " + n;
}
// Validate the form
function validateForm(x, y, z) {
var x = parseInt(document.forms["myForm"]["apr"].value);
var y = parseInt(document.forms["myForm"]["loanTerm"].value);
var z = parseInt(document.forms["myForm"]["loanAmount"].value);
// If statements
if (x==null || x=="") {
alert("APR must be filled out");
document.getElementById("apr").focus();
return false;
}
else if (y==null || y=="") {
alert("Loan Term must be filled out");
document.getElementById("loanTerm").focus()
return false;
}
else if (z==null || z=="") {
alert("Loan Amount must be filled out");
document.getElementById("loanAmount").focus()
return false;
}
else {
// Display
display(x, y, z);
}
return false;
}
//Reset the form (this isn't working)
function resetForm() {
document.getElementById("myForm").reset();
}
</script>
</head>
<body onLoad=document.getElementById("apr").focus();>
<form id="myForm" name="myForm" action="" onsubmit="validateForm(); return false;" method="post">
APR: <input type="number" id="apr" value=""><br/>
Loan Term: <input type="number" id="loanTerm" value=""><br/>
Loan Amount: <input type="number" id="loanAmount" value=""><br/>
<button onclick="myFunction()">Calculate Payment Button</button>
<input type="button" onclick="resetForm()" value="Reset form">
</form>
<div id="total"></div>
</body>
</html>
如果你'parseInt函数(“”)'返回'NaN'一个空字符串,我猜那是你的问题。编辑:所以你应该只需要添加'|| x ==“NaN”' – EvilZebra 2014-10-16 16:30:26
So l这个吗? if(x == null || x ==“”|| x ==“NaN”) – 2014-10-16 16:34:48
是的,应该这样做。 – EvilZebra 2014-10-16 16:35:03