Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在innerHTML中返回多个结果_Javascript_Return_Innerhtml_Getelementbyid - Fatal编程技术网

Javascript 在innerHTML中返回多个结果

Javascript 在innerHTML中返回多个结果,javascript,return,innerhtml,getelementbyid,Javascript,Return,Innerhtml,Getelementbyid,我是JavaScript新手,试图了解更多关于它能做什么以及如何使用它的知识 计算结果是否可以返回多个结果 我正在为一个班级项目做计算器。我希望它在我的页面上返回3个值: 利率 借款总额和 每月还款 到目前为止,我已经设法让它显示每月还款在一个div页上,但我想能够显示所有3页作为一个计算的结果 这可能吗 以下是我到目前为止的想法: HTML: JavaScript: function main() { var userInput1 = 0; var userInput2 = 0; var

我是JavaScript新手,试图了解更多关于它能做什么以及如何使用它的知识

计算结果是否可以返回多个结果

我正在为一个班级项目做计算器。我希望它在我的页面上返回3个值:

利率

借款总额
每月还款

到目前为止,我已经设法让它显示每月还款在一个div页上,但我想能够显示所有3页作为一个计算的结果

这可能吗

以下是我到目前为止的想法: HTML:

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; 

}
函数main()
{
var userInput1=0;
var userInput2=0;
var显示结果;
userInput1=document.getElementById(“贷款金额”).value;
userInput1=parseFloat(userInput1);
userInput2=document.getElementById(“贷款期限”).value;
userInput2=parseFloat(userInput2);
displayResult=calcLoan(userInput1,userInput2);
document.getElementById(“Result1”).innerHTML=displayResult;
}
函数calcLoan(userInput1,userInput2)
{
var利息=0;

如果(userInput1您可以创建具有多个自定义字段的变量,并在函数之间传递它们。因此,您的函数应该如下所示:

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; 
}
不要忘记添加ID为Result1、Result2和Result3的div元素

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


尝试一些JavaScript OOP。一个函数可以很容易地返回多个值。有几种方法可以实现。试着阅读以下内容:

谢谢,我会尝试一下,让您知道我尝试了这种方法。错误控制台给出错误“result id undefined”参考
var-result;result.interest=interest;result.totaloan=totaloan;result.monthly=monthly;
确定。将
var-result;
更改为
var-result=[];
。现在它应该可以工作了。它工作了。非常感谢您!代码>[]
mean?另外,如果我想包含一个复选框,该复选框进行了稍微不同的计算(添加了5%),我会将其放在单独的功能块中还是放在
calcLoan
块中?再次感谢!