我正在创建一个财务计算器。这是第一次。我希望表格将数字作为变量接收,并使用该变量表进行计算。我想使用表单,以便返回按钮可以提交条目。
当input
为函数使用onchange
侦听器时,此方法很好用。但是,当我尝试合并<input type="submit" />
时,DOM会以正确的计算方式进行更新,然后立即消失。
使用onsubmit
不能正常运行功能是什么?运行功能后,onsubmit
是否刷新DOM?
解决此类问题的最佳实践是什么?
谢谢, 会
HTML:
function update() {
let initialGain = Number(document.getElementById('initialGain').value);
let initialFedTax = initialGain * .238;
let capAvail = initialGain - initialFedTax;
let newValue = capAvail * (1+ .09 * 10);
let newValueB = initialGain * (1+ .09 * 10);
let tax85 = initialFedTax * .85
let tax2029 = (newValue - capAvail) * .238;
let afterTax = newValue - tax2029;
let afterTaxB = newValueB - tax85;
let netProfit = afterTax - initialGain;
let netProfitB = afterTaxB - initialGain;
// Capital Gain
document.getElementById('capitalGain').innerHTML = "$" + currency(initialGain);
document.getElementById('capitalGainB').innerHTML = "$" + currency(initialGain);
// Top Federal Tax Paid on Original Capital Gain, 23.8%
document.getElementById('initialFedTax').innerHTML = "$" + currency(initialFedTax);
// Capital Available for a "New Investment" in 2019
document.getElementById('capAvail').innerHTML = "$" + currency(capAvail);
document.getElementById('capAvailB').innerHTML = "$" + currency(initialGain);
// Value of New Investment After 10 Years
document.getElementById('newValue').innerHTML = "$" + currency(newValue);
document.getElementById('newValueB').innerHTML = "$" + currency(newValueB);
// Top Federal Tax on 85% of Original Gain due Dec 31, 2026
document.getElementById('tax85').innerHTML = "(" + currency(tax85) + ")";
// Top Federal Tax on Disposition of New Investment in 2029
document.getElementById('tax2029').innerHTML = "(" + currency(tax2029) + ")";
// After Tax Proceeds
document.getElementById('afterTax').innerHTML = "(" + currency(afterTax) + ")";
document.getElementById('afterTaxB').innerHTML = "(" + currency(afterTaxB) + ")";
// Less: Original Gain
document.getElementById('originalGain').innerHTML = "(" + currency(initialGain) + ")";
document.getElementById('originalGainB').innerHTML = "(" + currency(initialGain) + ")";
// Net Profit
document.getElementById('netProfit').innerHTML = "$" + currency(netProfit);
document.getElementById('netProfitB').innerHTML = "$" + currency(netProfitB);
var x = document.getElementsByClassName('test')
x[0].style.backgroundColor = 'red';
x[0].style.width = '10px';
x[0].style.height = '10px';
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="calculator.css">
<script src="calculator.js"></script>
</head>
<body>
<h1>
Calculator
</h1>
<form onsubmit=update()>
<input
type="number"
id="initialGain"
placeholder="$1,000,000" />
<input
type="submit"
value="click" />
</form>
<input
type="checkbox"
id="toggle" />
<table>
<caption>After-Tax Net Profit Investment Comparison</caption>
<thead>
<tr>
<td></td>
<td>TAXABLE GAINS<br/>(Long Term)</td>
<td>1 American Elite™</td>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Capital Gain</strong></td>
<td id="capitalGain"></td>
<td id="capitalGainB"></td>
</tr>
<tr>
<td><strong>Top Federal Tax Paid on Original Gain</strong><br/><em>23.8%</em></td>
<td id="initialFedTax"></td>
<td id="rLong">-</td>
</tr>
<tr>
<td><strong>Capital Available for a "New Investment" in 2019</strong></td>
<td id="capAvail"></td>
<td id="capAvailB"></td>
</tr>
<tr>
<td><strong>Value of New Investment after 10 years</strong> <br/> <em>9% Simple</em></td>
<td id="newValue"></td>
<td id="newValueB"></td>
</tr>
<tr>
<td><strong>Top Federal Tax on 85% of Original Gain due Dec 31, 2026</strong><br/> <em>23.8%</em></td>
<td></td>
<td id="tax85"></td>
</tr>
<tr>
<td><strong>Top Federal Tax on Disposition of New Investment in 2029</strong><br/> <em>23.8%</em></td>
<td id="tax2029"></td>
<td></td>
</tr>
<tr>
<td><strong>After Tax Proceeds</strong></td>
<td id="afterTax"></td>
<td id="afterTaxB"></td>
</tr>
<tr>
<td>Less: Original Gain</td>
<td id="originalGain"></td>
<td id="originalGainB"></td>
</tr>
<tr>
<td><strong>Net Profit</strong><br/><em>(After Federal Tax on Original Gain and New Investment)</em></td>
<td id="netProfit"></td>
<td id="netProfitB"></td>
</tr>
</tbody>
</table>
<div class="test"></div>
</body>
</html>
答案 0 :(得分:2)
是的,默认情况下,onsubmit
通过HTTP发送表单内容以进行服务器端处理,因此刷新了站点。为避免这种情况,请将return false;
作为更新函数的最后一条语句,然后将onsubmit=update()
替换为onsubmit="return update();"
要使其正常工作,您的函数必须没有错误。否则,将无法到达return false;
行,并且浏览器会刷新页面。