我是JavaScript新手,试图了解更多关于它可以做什么以及如何使用它的知识。在innerHTML中返回多个结果
是否可以返回多个结果作为计算的结果?
我正在为班级项目计算器。我想它做的是我的页面上的返回3个值:
Interest rate
total amount borrowed
和 monthly repayment
到目前为止,我已经成功地得到它在一个div在页面上显示每月还款,但我希望能够通过一次计算显示页面上的所有3个。
这可能吗?
以下是我想出迄今: HTML: <p><input type="button" onclick="main();" value="Calculate"></p>
的JavaScript:
function main()
{
var userInput1 = 0;
var userInput2 = 0;
var displayResult;
userInput1 = document.getElementById("loan_amount").value;
userInput1 = parseFloat(userInput1);
userInput2 = document.getElementById("loan_term").value;
userInput2 = parseFloat(userInput2);
displayResult = calcLoan(userInput1,userInput2);
document.getElementById("Result1").innerHTML=displayResult;
}
function calcLoan(userInput1,userInput2)
{
var interest =0;
if (userInput1 <1000)
{
alert("Please enter a value above £1000")
}
else if (userInput1 <= 10000)
{
interest = 4.25 + 5.5;
}
else if (userInput1 <= 50000)
{
interest = 4.25 + 4.5;
}
else if (userInput1 <= 100000)
{
interest = 4.25 + 3.5;
}
else
{
interest = 4.25 + 2.5;
}
var totalLoan = 0;
totalLoan = userInput1 +(userInput1*(interest/100))*userInput2;
var monthlyRepayment = 0;
var monthly;
monthlyRepayment = totalLoan/(userInput2*12);
monthly=monthlyRepayment.toFixed(2);
alert("Interest Rate = " + interest + "%" +" "+"Total Loan amount = " + "£"+ totalLoan +" "+ "Your monthly repayment is = " + " " + "£"+ monthly);
return monthly;
}
如果任何人都可以在正确的方向指向我,那就太好了!
谢谢,我会给它一个并让你知道 – user1361276
我试过这种方法。错误控制台提供错误“result id undefined”引用var result; result.interest = interest; result.totalLoan = totalLoan; result.monthly = monthly;' – user1361276
OK。将'var result;'更改为'var result = [];'。现在它应该工作。 – mostar