我正在构建一个简单的应用程序,可将 Ether 转换为其各种单位。我对其进行了设置,以便在输入框中输入数字时,我的转换函数会运行并显示到所需单位的转换(可以在下拉列表中选择)。
我遇到的问题是该函数仅在页面加载时运行。在此之后,不管我输入,显示的转换值不更改默认,即使我有转换功能设置为运行oninput,onchange以及onclick就该接受数据的HTML元素。
我通过将脚本放在 HTML 文件的最底部来确保我的脚本在页面完全加载后运行,但这根本没有帮助。
HTML:
<div id="conversions">
<p>
<input type="number" id="etherAmount" value="2"> ether to <select id="etherUnits">
<option id="wei" value="wei">Wei</option>
<option id="mwei" value="mwei">Mwei/Lovelace/Picoether</option>
<option id="gwei" value="gwei">Gwei/Shannon/Nanoether/Nano</option>
<option id="szabo" value="szabo">Szabo/Microether/Micro</option>
<option id="finney" value="finney">Finney/Milliether/Milli</option>
<option id="ether" value="ether">Ether</option>
<option id="kether" value="kether">Kether/Grand</option>
<option id="mether" value="mether">Mether</option>
<option id="gether" value="gether">Gether</option>
<option id="tether" value="tether">Tether</option>
<input type="submit" value="Convert" id="convert">
</select>
</p>
</div>
<div id="resultsContainer">
<p id="results"></p>
</div>
JS:
const converter = require("ethereumjs-units")
let etherUnits = document.getElementById("etherUnits")
let etherAmount = document.getElementById("etherAmount")
let convertButton = document.getElementById("convert")
let results = document.getElementById("results")
etherUnits.oninput = convert()
etherAmount.onchange = convert()
etherAmount.oninput = convert()
convertButton.onclick = convert()
//Takes value of ether input box and converts it
function convert() {
//value of "convert to" box
let etherUnitVal = etherUnits.options[etherUnits.selectedIndex].value
results.innerHTML = converter.lazyConvert(etherAmount.value.toString() + " eth", etherUnitVal)
}
慕容森
喵喵时光机
相关分类