试图制作一个表格来计算复合利息

时间:2016-10-12 04:03:11

标签: javascript html forms

我在这里发现了不同类型的片段,计算兴趣等等但无法找到我需要的帮助。

我制作一个简单的表单并使用Javascript查找复合的兴趣并以HTML格式输出。

我使用带有和不带innerHTML的getDocumentById以及将它放在parseInt()中。长话短说,我得到了NaN'作为输出。

看看我最终完成的事情,也许有人可以指出我可能缺少的东西。感谢

<form onsubmit="return false">
    <input id="a" name="a" type="number" step="any" placeholder="Present Value"> * (
    <input id="b" name="b" type="number" step="any" placeholder="Number of years"> +
    <input id="c" name="c" type="number" step="any" placeholder="Interest Rate">)^
    <input id="d" name="d" type="number" step="any" placeholder="N number of Years">=
    <button onclick="futureNyears()">Answer</button>
    <p id="futureNanswer"></p>
</form>
<script type="text/javascript">
    function futureNyears() {
        var a = parseInt(document.getElementById('a'));
        var b = parseInt(document.getElementById('b'));
        var c = parseFloat(document.getElementById('c'));
        var d = parseInt(document.getElementById('d'));

        var E = eval(b+c);
        var r = Math.pow(E, d);
        var f = eval(E * r);
        var g = f.toString();

        document.getElementById("futureNanswer").innerHTML = g;
    }
</script>

5 个答案:

答案 0 :(得分:3)

document.getElementById('a')返回dom元素,因此您必须从中获取值,如下所示

var a = parseInt(document.getElementById('a').value); 

答案 1 :(得分:3)

按ID获取元素后,需要访问value属性,parseInt一直接收等于NaN的DOM对象。

function futureNyears() {
        var a = parseInt(document.getElementById('a').value);
        var b = parseInt(document.getElementById('b').value);
        var c = parseFloat(document.getElementById('c').value);
        var d = parseInt(document.getElementById('d').value);

        var E = (b+c);
        var r = Math.pow(E, d);
        var f = (E * r);
        var g = f.toString();

        document.getElementById("futureNanswer").innerHTML = g;
    }
<form onsubmit="return false">
    <input id="a" name="a" type="number" step="any" placeholder="Present Value"> * (
    <input id="b" name="b" type="number" step="any" placeholder="Number of years"> +
    <input id="c" name="c" type="number" step="any" placeholder="Interest Rate">)^
    <input id="d" name="d" type="number" step="any" placeholder="N number of Years">=
    <button onclick="futureNyears()">Answer</button>
    <p id="futureNanswer"></p>
</form>

答案 2 :(得分:3)

在您的情况下,您忘记编写document.getElementById(&#39; a&#39;)。value(),因为它是一个表单元素,如果没有value()属性,则无法提取其值。 Parseint()始终返回字符串中检测到的第一个数字。

答案 3 :(得分:2)

您需要使用.value属性来返回特定元素的值。

var a = parseInt(document.getElementById('a').value);

value属性设置或返回选项的值(提交表单时要发送到服务器的值)

<强> HTML

<!DOCTYPE html>
<html>
<head>
    <title>CI</title>
</head>
<body>
    <form onsubmit="return false">
    <input id="a" name="a" type="number" step="any" placeholder="Present Value"> * (
    <input id="b" name="b" type="number" step="any" placeholder="Number of years"> +
    <input id="c" name="c" type="number" step="any" placeholder="Interest Rate">)^
    <input id="d" name="d" type="number" step="any" placeholder="N number of Years">=
    <button onclick="futureNyears()">Answer</button>
    <p id="futureNanswer"></p>
</form>
</body>
</html>

<强>的JavaScript

<script type="text/javascript">
    function futureNyears() {
        var a = parseInt(document.getElementById('a').value);
        var b = parseInt(document.getElementById('b').value);
        var c = parseFloat(document.getElementById('c').value);
        var d = parseInt(document.getElementById('d').value);

        var E = eval(b+c);
        var r = Math.pow(E, d);
        var f = eval(E * r);
        var g = f.toString();

        document.getElementById("futureNanswer").innerHTML = g;
    }
</script>

答案 4 :(得分:1)

为了从输入文本框中获取值,请使用

var a = parseInt(document.getElementById('a').value);  

而不是

var a = parseInt(document.getElementById('a'));  

&#13;
&#13;
<form onsubmit="return false">
    <input id="a" name="a" type="number" step="any" placeholder="Present Value"> * (
    <input id="b" name="b" type="number" step="any" placeholder="Number of years"> +
    <input id="c" name="c" type="number" step="any" placeholder="Interest Rate">)^
    <input id="d" name="d" type="number" step="any" placeholder="N number of Years">=
    <button onclick="futureNyears()">Answer</button>
    <p id="futureNanswer"></p>
</form>
<script type="text/javascript">
    function futureNyears() {
        var a = parseInt(document.getElementById('a').value);
        var b = parseInt(document.getElementById('b').value);
        var c = parseFloat(document.getElementById('c').value);
        var d = parseInt(document.getElementById('d').value);

        var E = eval(b+c);
        var r = Math.pow(E, d);
        var f = eval(E * r);
        var g = f.toString();

        document.getElementById("futureNanswer").innerHTML = g;
    }
</script>
&#13;
&#13;
&#13;