简体   繁体   English

在innerHTML中返回多个结果

[英]Returning multiple results in innerHTML

I'm new to JavaScript and trying to learn more about what it can do and how to use it. 我是JavaScript的新手,正在尝试进一步了解它可以做什么以及如何使用它。

is it possible to return multiple results as a result of a calculation? 计算结果是否可以返回多个结果?

I am working on a calculator for a class project. 我正在为一个班级项目设计计算器。 What I would like it to do is return 3 values on my page: 我想要做的是在页面上返回3个值:

Interest rate

total amount borrowed and monthly repayment total amount borrowedmonthly repayment

So far I have managed to get it to display monthly repayment in a div on the page, but I would like to be able to show all 3 on the page as a result of the one calculation. 到目前为止,我设法使它在页面上的div中显示每月还款额,但由于一次计算,我希望能够在页面上显示所有3个。

Is this possible? 这可能吗?

Here is what I have come up with so far: HTML: <p><input type="button" onclick="main();" value="Calculate"></p> 到目前为止,这是我提出的内容:HTML: <p><input type="button" onclick="main();" value="Calculate"></p> <p><input type="button" onclick="main();" value="Calculate"></p>

JavaScript: 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; 

}

If anyone can point me in the right direction, it would be great! 如果有人能指出我正确的方向,那就太好了!

You can create variables with multiple custom fields and pass them between functions. 您可以创建具有多个自定义字段的变量,然后在函数之间传递它们。 So, your functions should look like this: 因此,您的函数应如下所示:

function main()
{
    ...

    displayResult = calcLoan(userInput1,userInput2);
    document.getElementById("Result1").innerHTML = displayResult.interest;
    document.getElementById("Result2").innerHTML = displayResult.totalLoan;
    document.getElementById("Result3").innerHTML = displayResult.monthly;
}

function calcLoan(userInput1,userInput2)
{
    ...

    alert("Interest Rate = " + interest + "%" +" "+"Total Loan amount = " + "£"+ totalLoan +" "+ "Your monthly repayment is = " + " " + "£"+ monthly);

    var result;
    result.interest = interest;
    result.totalLoan = totalLoan;
    result.monthly = monthly;

    return result; 
}

And don't forget to add div elements with IDs Result1, Result2 and Result3. 并且不要忘记添加ID为Result1,Result2和Result3的div元素。

<div id="Result1"></div>
<div id="Result2"></div>
<div id="Result3"></div>

Try some JavaScript OOP. 尝试一些JavaScript OOP。 A function can return multiple values quite easily. 一个函数可以很容易地返回多个值。 There are several ways to do it. 有几种方法可以做到这一点。 Try to read this: http://killdream.github.com/blog/2011/10/understanding-javascript-oop/ and this: http://net.tutsplus.com/tutorials/javascript-ajax/the-basics-of-object-oriented-javascript/ 尝试阅读以下内容: http : //killdream.github.com/blog/2011/10/understanding-javascript-oop/和以下内容: http : //net.tutsplus.com/tutorials/javascript-ajax/the-basics-面向对象的javascript /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM