2012-05-08 3 views
0

저는 JavaScript를 처음 사용하고이를 통해 얻을 수있는 효과와 그 활용 방법에 대해 알고 있습니다.innerHTML에서 여러 결과 반환

계산 결과로 여러 결과를 반환 할 수 있습니까?

나는 수업 프로젝트를 위해 계산기를 연구 중이다. 내가 그것을 할 싶습니다 내 페이지에 반환 3 개 값이다 :

Interest rate

total amount borrowedmonthly repayment

지금까지 내가 페이지에 사업부에서 매달 상환을 표시 그걸 얻기 위해 관리해야 ,하지만 한 계산의 결과로 페이지에 모두 3을 표시 할 수 있기를 바랍니다.

이것이 가능합니까? 여기

내가 지금까지 함께 온 것입니다 : HTML : <p><input type="button" onclick="main();" value="Calculate"></p>

자바 스크립트 : 사람이 올바른 방향으로 날 지점 수 있다면

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; 

} 

것은, 그것은 좋은 것입니다!

답변

1

사용자 정의 필드가 여러 개인 변수를 만들어 함수간에 전달할 수 있습니다.

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를 결과 1, Result2 및 Result3와 DIV 요소를 추가하는 것을 잊지 마세요 : 그래서, 당신의 기능은 다음과 같이한다.

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

감사합니다. 나는 그것을 줄 것이고 당신에게 알려줄 것입니다. – user1361276

+0

이 방법을 시도했습니다. 오류 콘솔은'var result '를 참조하는 "result id undefined"오류를 제공합니다. result.interest = interest; result.totalLoan = totalLoan; result.monthly = monthly; ' – user1361276

+0

확인. 'var result;를'var result = [];'로 변경하십시오. 이제는 효과가 있습니다. – mostar