2016-04-05 14 views
0

Ich muss eine einfache Autokreditberechnung mit HTML, und übergeben Sie alle Informationen zu einem anderen JSP-Seite. Das Problem ist, ich weiß nicht, wie ich meine Javascript-Berechnungsmethode in ein Formular in jsp übergeben kann. Irgendwelche Ideen oder Empfehlungen würden sehr geschätzt werden! Habe ich meine if else-Anweisung auch richtig gemacht? Ich bin neu bei Jsp, also entschuldige bitte, wenn ich zu ahnungslos bin. :)JSP, HTML und Javascript - Passing Berechnung zu Jsp

Das ist mein HTML-Code:

<html> 
    <head> 
    <title>Car Loan Calculation</title> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
     <style> 
     label { 
      text-align:left; 
      width:130px; 
      display:inline-block; 
      padding-top:10px; 
     } 
     </style> 
    </head> 
    <body> 
     <h1>Perform Car Loan Calculation</h1> 
      <form id ="memberFrm" action="processCalculateCarLoan.jsp" method="post" onsubmit="return calculate()"> 
          <fieldset> 
            <legend>Member Registration</legend><br> 

            <label for="loan">Loan Amount*</label> 
            <input type="text" id="loan" name="my_loan" size ="15" placeholder="Amount" /> 
            <br/> </br> 

            <label>Period</label> 
            <select id="period" name="my_period"> 
             <option value="1">3 years</option> 
             <option value="2">4 years</option> 
             <option value="3">5 years</option> 
             <option value="4">7 years</option> 
            </select> 
            <br/> </br> 

            <p> <input type="submit" id="btnSubmit" value="Submit"/> 
             <input type="reset" id="btnCancel" value="Cancel"/> 
            </p> 
          </fieldset> 

      </form> 
     <script> 
      function calculate(){ 
       var e = document.getElementById("period"); 
       var years = e.options[e.selectedIndex].value; 
       var loans = document.getElementById("loan").value; 
       var calc= document.getElementById("ans"); 

       if (years === "1"){ 
        var total = ((0.28 * 3)+loans); 
       } 
       else if (years === "2"){ 
        var total = ((0.28 * 4)+loans); 
       } 
       else if (years === "3"){ 
        var total = ((0.28 * 5)+loans); 
       } 
       else if (years === "4"){ 
        var total = ((0.28 * 7)+loans); 
       } 


      } 
     </script> 

    </body> 
</html> 

Dies ist mein JSP-Code:

<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body> 
     <h1>Car Loan Calculation</h1> 
       <fieldset> 
          <% 
           String myLoan = null; 
           String myPeriod = null; 


           myLoan = request.getParameter("my_loan"); 
           myPeriod = request.getParameter("my_period"); 
          %> 

          <p>Thank you for registering in this event!</p> 
          <p>This is your details;</p> 
          <p>Loan Amount : <%=myLoan%></p> 
          <p>Loan Period : <%=myPeriod%></p> 
          <p>Loan Total : <%=%></p> <!-- I need to display the total calculation here but I don't know how to pass the value. Any ideas?--> 


       </fieldset> 
    </body> 
</html> 

Antwort

0

Die in der HTML-Seite, dh gesammelten Daten; Zeitraum und Darlehen sollte an die JSP-Seite weitergegeben werden und die Darlehensberechnungslogik in der JSP-Seite tun und dann dort anzeigen. Dies hält die Geschäftslogik (Darlehen Berechnung) vom Endbenutzer getrennt.