简体   繁体   English

如何创建一个javascript表并从输入中获取计算作为表数据

[英]How to create a javascript table and get a calculations from input as table data

I'm creating a table using Javascript. 我正在使用Javascript创建一个表。 I have 2 input; 我有2个输入; a principle amount and an annual interest rate. 原则金额和年利率。 How can I create a loop with an output into a table of 10 years with principle+compounded interest rate. 如何使用原则+复利率创建一个输出为10年表的循环。 I'm not allowed to use jquery. 我不允许使用jquery。

**I'm trying to display results as a table of the increased amount based on the principle and interest (compound interest); **我试图根据原则和兴趣(复利)显示结果作为增加数量的表格; Year 1 Year 2 Year 3 Year 4 Year 5 1100 1210 1331 1464 1610 (10 years in a table, first row are years 1 to 10, second row are the amounts, sorry I don't know how to add a picture here) 1年级2年级3年级4年级5 1100 1210 1331 1464 1610(表中10年,第1行是1年级到10年级,第2行是金额,抱歉我不知道如何在这里添加图片)

Here is what I have started so far for the form input; 这是我到目前为止开始的表单输入;

<!DOCTYPE html>
<html>

  <head>
    <title>Savings Account Calculator</title>   

  </head>

  <body>
    <h1>Savings Account Calculator</h1>

  <p>

    <form name="savingdata">
    <table>

    <tr>
    <td>Principle Amount:</td> 
    <td><input type="text" id="principle" value="1000" /></td>
    </tr>

    <tr>
    <td>Annual Interest (%):</td> 
    <td><input type="text" id="interest" value="10" /></td>
    </tr>

    </table>
    <br>
    <input type="button" onClick="calculateBy()" Value="Calculate" />
    </form>
    <br>
  </p>
</body>
</html>

I don't really get what you want to do but with this javascript, you'll get the two values you're interested in in the calculateBy function. 我真的不知道你想要做什么但是使用这个javascript,你会在calculateBy函数中获得你感兴趣的两个值。 Up to you to make the right computation then. 由你决定正确的计算。

function calculateBy() {
  var interest = document.getElementById("interest").value;
  var principle = document.getElementById("principle").value;

  alert("Interest: "+interest+", principle: "+principle);
 /* calculation here */
}

jsFiddle here: https://jsfiddle.net/1L69msya/1/ jsFiddle这里: https ://jsfiddle.net/1L69msya/1/

You can setup an empty div and use a table or other HTML element and populate the results with that. 您可以设置一个空div并使用表或其他HTML元素并使用该元素填充结果。

JSFiddle - https://jsfiddle.net/xjj7vook/ JSFiddle - https://jsfiddle.net/xjj7vook/

function calculateBy() {

  var principle = document.getElementById('principle').value;
  var interest = document.getElementById('interest').value;
  interest = 1 + (interest / 100); //convert to dec
  var tableHTML = "<table>";
  alert(interest);
  var total = principle;
  for (i = 1; i < 10; i++) {
    var res = principle * (Math.pow(interest, i));
    res = res.toFixed(2);
    interestgained = +res - +principle;
    interestgained = interestgained.toFixed(2);
    tableHTML = tableHTML + "<tr>Year " + i + " | Principle: " + principle + " USD | Total: " + res + " USD | Interest: " + interestgained + " USD</tr><br>";
  }

  tableHTML = tableHTML + "</table>";
  alert(tableHTML);
  document.getElementById("someDIV").innerHTML = tableHTML;
}

Formula is not correct, I don't know what you are trying to calculate. 公式不正确,我不知道你想要计算什么。 This one just shows you how much interest you would gain per years. 这个只是向您展示您每年会获得多少兴趣。

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

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