我正在学习javascript而且我坚持这个。似乎没有任何语法错误,并且表单在html中正确显示,我只是没有获得任何计算数据。我不确定我是否遗漏了一些你可能注意到的小东西,或者我写的是不是完全错了。任何建议将不胜感激。
感谢您的帮助
HTML:
<form id="loanddata">
<table>
<tr><td><b>enter loan information</b></td></tr>
<tr><td>1) Amount of loan</td> <td><input type="text" name="principal" onchange="calculate();"></td></tr>
<tr><td>2) Annual Percent of increase</td> <td><input type="text" name="interest" onchange="calculate();"></td></tr>
<tr><td>3) Repayment period in years</td><td><input type="text" name="years" onchange="calculate();"></td></tr>
<tr><td></td>
<td><button name="compute" type="submit" onclick="calculate();"></td>
</tr>
<tr><td><b>Payment Information</b></td></tr>
<tr><td>4) Your Monthly Payment</td> <td>$<span class="result" id="payment"></span></td></tr>
<tr><td>5)Your Total Payment</td> <td>$<span class="result" id="total"></span></td></tr>
<tr><td>6) Your Total Interest Payment</td> <td><span class="result" id="totalinterest"></td> </tr>
</table>
</form>
JavaScript的:
function calculate() {
var principal = document.loandata.principal.value;
var interest = document.loandata.interest.value;
var payments = document.loandata.years.value;
var x = Math.pow(1 + interest, payments);
var monthly = (principal*x*interest)/(x-1);
var payment = document.getElementById("payment");
var total = document.getElementById("total");
var totalinterest = document.getElementById("totalinterest");
if (isFinite(monthly)){
payment.innerHTML = monthly.toFixed(2);
total.innerHTML = (monthly * payments).toFixed(2);
totalinterest.innerHTML = ((monthly*payments)-principal).toFixed(2);
} else {
payment.innerHTML = "";
total.innerHTML = "";
totalinterest.innerHTML = "";
}
}
答案 0 :(得分:0)
试试这个:
<强> HTML:强>
<table>
<tr><td><b>enter loan information</b></td></tr>
<tr><td>1) Amount of loan</td> <td><input type="text" id="principal" onchange="calculate();"></td></tr>
<tr><td>2) Annual Percent of increase</td> <td><input type="text" id="interest" onchange="calculate();"></td></tr>
<tr><td>3) Repayment period in years</td><td><input type="text" id="years" onchange="calculate();"></td></tr>
<tr><td></td>
<td><button name="compute" type="submit" onclick="calculate();" /></td>
</tr>
<tr><td><b>Payment Information</b></td></tr>
<tr><td>4) Your Monthly Payment</td> <td>$<span class="result" id="payment"></span></td></tr>
<tr><td>5)Your Total Payment</td> <td>$<span class="result" id="total"></span></td></tr>
<tr><td>6) Your Total Interest Payment</td> <td><span class="result" id="totalinterest"></td></tr>
</table>
<强> JavaScript的:强>
function calculate() {
var principal = parseInt(document.getElementById("principal").value);
var interest = parseInt(document.getElementById("interest").value);
var payments = parseInt(document.getElementById("years").value);
if(!isNaN(principal) && !isNaN(interest) && !isNaN(payments)){
var x = Math.pow(1 + interest, payments);
var monthly = (principal*x*interest)/(x-1);
var payment = document.getElementById("payment");
var total = document.getElementById("total");
var totalinterest = document.getElementById("totalinterest");
if (isFinite(monthly)){
payment.innerHTML = monthly.toFixed(2);
total.innerHTML = (monthly * payments).toFixed(2);
totalinterest.innerHTML = ((monthly*payments)-principal).toFixed(2);
} else {
payment.innerHTML = "";
total.innerHTML = "";
totalinterest.innerHTML = "";
}
}
}
正在使用 Fiddle.
答案 1 :(得分:0)
变化:
<form id="loanddata">
有关:
<form name="loandata">
答案 2 :(得分:0)
我修改了你的代码 有一些拼写错误和基本的javascript错误。
第一个表单ID将是loandata。
第二次访问这样的表单值 - document.forms.loandata.principal.value;
最后 - 添加此项以限制表单提交 - onsubmit =“return 假;“
这是工作代码。
HTML -
<form id="loandata" onsubmit="return false;">
<table>
<tr><td><b>enter loan information</b></td></tr>
<tr><td>1) Amount of loan</td> <td><input type="text" name="principal" onchange="calculate();"></td></tr>
<tr><td>2) Annual Percent of increase</td> <td><input type="text" name="interest" onchange="calculate();"></td></tr>
<tr><td>3) Repayment period in years</td><td><input type="text" name="years" onchange="calculate();"></td></tr>
<tr>
<td></td>
<td><button name="compute" onclick="calculate();"></td>
</tr>
<tr><td><b>Payment Information</b></td></tr>
<tr><td>4) Your Monthly Payment</td> <td>$<span class="result" id="payment"></span></td></tr>
<tr><td>5)Your Total Payment</td> <td>$<span class="result" id="total"></span></td></tr>
<tr><td>6) Your Total Interest Payment</td> <td><span class="result" id="totalinterest"></td></tr>
</table>
</form>
JS -
function calculate() {
var principal = document.forms.loandata.principal.value;
var interest = document.forms.loandata.interest.value;
var payments = document.forms.loandata.years.value;
var x = Math.pow(1 + interest, payments);
var monthly = (principal * x * interest) / (x - 1);
var payment = document.getElementById("payment");
var total = document.getElementById("total");
var totalinterest = document.getElementById("totalinterest");
if (isFinite(monthly)) {
payment.innerHTML = monthly.toFixed(2);
total.innerHTML = (monthly * payments).toFixed(2);
totalinterest.innerHTML = ((monthly * payments) - principal).toFixed(2);
} else {
payment.innerHTML = "";
total.innerHTML = "";
totalinterest.innerHTML = "";
}
}
确保在头标记上附加JS。