【问题标题】:AppendChild and for loop issueAppendChild 和 for 循环问题
【发布时间】:2012-04-13 05:37:04
【问题描述】:

我有两个我无法弄清楚的问题。我正在使用 javascript 来计算复利。我的计算利息的公式有效,但我需要在计算之后出现一个表格利率表(以逐期显示每次应计的金额),然后是最终的总利息金额和总利息金额。这是我的代码的样子:

<head>
    <title></title>
<script type="text/javascript">
    function rateSched(amnt, prd, rte, namt) {
        for (i = 0; i < periods; i++) {
            if (!document.getElementsByTagName) return;
            tabBody = document.getElementsByTagName("tbody").item(0);
            row = document.createElement("tr");
            cell1 = document.createElement("td");
            cell2 = document.createElement("td");
            cell3 = document.createElement("td");
            cell4 = document.createElement("td");
            textnode1 = document.createTextNode(amnt);
            textnode2 = document.createTextNode(prd);
            textnode3 = document.createTextNode(rte);
            textnode4 = document.createTextNode(namt);
            cell1.appendChild(textnode1);
            cell2.appendChild(textnode2);
            cell3.appendChild(textnode3);
            cell4.appendChild(textnode4);
            row.appendChild(cell1);
            row.appendChild(cell2);
            row.appendChild(cell3);
            row.appendChild(cell4);
            tabBody.appendChild(row);
        } 
    }
    var deposit;
    var periods;
    var interest;
    var newamount = deposit * (Math.pow(1 + interest, periods) - 1) / interest;
    var i=0;

    function calcAmt(form1) {
        deposit = form1.dp.value;
        interest = form1.rt.value;
        periods = form1.pr.value;
        form1.amt.value = Math.round(deposit * (Math.pow((1 + interest / 100), periods)) * 100) / 100;
        form1.intr.value = Math.round((deposit * (Math.pow((1 + interest / 100), periods)) - deposit) * 100) / 100
    }
</script>
</head>
<body>
<center>
    <form method="get" name="form1" onreset="form1.dp.focus()">

    <table border="5" bgcolor="#BDBDBD" cellpadding="2" width="300px">
    <tr><td colspan="2" align="center">
    <b><font size="5" color="#4B088A">Compound Interest</font></b>
    </td></tr>

    <tr><td align="right">Initial Deposit</td><td>
    <input type="text" name="dp" value=""/></td></tr>

    <tr><td align="right">Interest Rate (% Value)</td><td>
    <input type="text" name="rt" value=""/></td></tr>

    <tr><td align="right">Periods</td><td>
    <input type="text" name="pr" value=""/></td></tr>

    <tr><td align="right">Final Amount</td><td>
    <input type="text" name="amt" value=""/></td></tr>

    <tr><td align="right">Total Interest</td><td>
    <input type="text" name="intr" value=""/></td></tr>

    <tr><td>
    <input type="button" name="calc" value="Calculate" size="13" onclick="calcAmt(form1);rateSched(deposit,interest,i,newamount);return false;"/></td>

    <td></td></tr>
    </table>
    </form>

    <br />

    <table border='1' width="300px">
    <tbody>
    <tr><td>Amount</td><td>Period</td><td>Rate</td><td>New Amount</td></tr>
    </tbody>
    </table>

    <br />

    <table>
    <tr><td align="right">Final Amount</td><td>
    <input type="text" name="amt" value="" size="13"/></td></tr>

    <tr><td align="right">Total Interest</td><td>
    <input type="text" name="intr" value="" size="13"/></td></tr>
    </table>

    </center>
</body>
</html>

我的问题是新表格行应该附加到较低的四列表格而不是上部表格,它们应该显示每个期间而不是相同的列,以及底部的“最终金额”和“总计兴趣”不用onclick填写。

编辑:最新版本的代码有问题,如 cmets 所述:

function calcAmt(form1) { 
    var newamount = deposit * (Math.pow(1 + interest, periods) - 1) / interest; 
    deposit = form1.dp.value; 
    interest = form1.rt.value; 
    periods = form1.pr.value; 
    form1.amt.value = Math.round(deposit * (Math.pow((1 + interest / 100), periods)) * 100) / 100; 
    form1.intr.value = Math.round((deposit * (Math.pow((1 + interest / 100), periods)) - deposit) * 100) / 100 
}

【问题讨论】:

    标签: javascript forms function for-loop appendchild


    【解决方案1】:

    要定位正确的tbody 标记,您应该在其上添加一个ID 并使用document.getElementById("xxx"),因为这将保证您获得正确的标记,而无需依赖HTML 文档中的特定位置索引。

    仅供参考,所有表格都有一个tbody 标签,无论它是否在您的 HTML 中。

    您可以在此处查看我如何使用 document.getElementById("results") 将结果放入正确的表中:http://jsfiddle.net/jfriend00/MqMtd/

    此外,您打算成为局部变量(而不是全局变量)的函数内的所有变量都应该在它们前面有 var。您使用了很多隐式全局变量(从未显式声明的变量),这是一种非常糟糕的做法。

    newAmount 的值无效,因为初始化它的代码在它所依赖的变量填充之前运行。我建议您在 calcAmt() 中设置 newAmount 的值,其中其他值它取决于设置。

    【讨论】:

    • 由于某种原因,当我尝试在 calcAmt() 中设置 newAmount 值时,我的表不再被附加。你知道为什么会这样吗?
    • @user1330759 - 您可能会导致带有无效代码的 javascript 错误并且正在停止执行。如果没有看到确切的代码,我真的无法就具体问题提出建议。
    • 其余代码保持不变,但我将 newAmount var 移动到 calcAmt() 函数中,如下所示: 'function calcAmt(form1) { var newamount = deposit * (Math.pow(1 + 利息,期间) - 1) / 利息;存款 = form1.dp.value;利息 = form1.rt.value;句点 = form1.pr.value; form1.amt.value = Math.round(存款 * (Math.pow((1 + 利息 / 100), 周期)) * 100) / 100; form1.intr.value = Math.round((存款 * (Math.pow((1 + 利息 / 100), 周期)) - 存款) * 100) / 100 }'
    • @user1330759 - 多行代码在评论中完全不可读。您应该始终使用edit 链接将多行代码放入您的问题或答案中,然后发表评论,将人们引导至新内容。我将此代码放入您的问题中,现在将对其进行查看。
    • @user1330759 - 将newamount 代码行移动到calcAmt() 的末尾。这取决于depositinterestperiods 的值,在设置这些变量之前,您无法计算newamount
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多