将变量从 HTML 传递到 JS 回 HTML

我需要将计算的会员费添加到我的业余无线电俱乐部的会员申请页面。从我读到的内容来看,我需要使用 ID。但我仍然无法让它工作。下面是包含 javascript 的页面的一部分。任何帮助是极大的赞赏。


                    <tr>

                        <td width="5%">&nbsp;</td>

                        <td class="bosytext" width="15%">Membership Period</td>

                            <td class="bosytext" width="75%"><input type="number" name="membershipterm"  id="term" required="required"> $22 for single year membership/ $20 per multi-year membership</td>

                            <td width="5%">&nbsp;</td>

                    </tr>

                    <tr>

                        <td width="5%">&nbsp;</td>

                        <td width="15%">Fee</td>

                        <script>

                    var fee;

                    var term;


                    if term = 1 {

                        (fee = 22;

                    } else {

                        fee = 20;

                    }

                    //calculates membership fee

                    var totalFee = (fee * term)

                    // outputs calced membership fee to html

                            document.getElementById('totalFee')

                    </script>

                        <td width="75%">id="totalFee</td>

                        <td width="5%">&nbsp;</td>

                    </tr>


守着星空守着你
浏览 263回答 2
2回答

慕桂英546537

在 HTMLid中用作指向 HTML 元素的链接。您将其设置在要引用的标签中:<td width="75%" id="totalFee></td>然后您可以获取该标签并从 js 操作它:let total_Fee = (fee * term);document.getElementById('totalFee').value = total_Fee;

子衿沉夜

您的代码有很多语法错误。尝试这个:<html><body><table style="width:100%">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="bosytext" width="15%">Membership Period</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="bosytext" width="75%">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="number" name="membershipterm" id="term" required="required">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button onclick="calc()">calc</button>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $22 for single year membership/ $20 per multi-year membership </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td width="15%">Fee</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td width="75%" id="totalFee"></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr></table>&nbsp; <script>&nbsp; &nbsp; function calc(){&nbsp; &nbsp; &nbsp; &nbsp; var term = document.getElementById('term').value;&nbsp; &nbsp; &nbsp; &nbsp; var fee;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (term == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fee = 22;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fee = 20;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var totalFee = (fee * term);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('totalFee').innerHTML = totalFee&nbsp; &nbsp; }&nbsp; </script>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript