Javascript 函数未运行 oninput() 或 onchange()

我正在构建一个简单的应用程序,可将 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)

}


陪伴而非守候
浏览 163回答 2
2回答

慕容森

我猜你的问题是你不是分配一个由那些事件处理程序执行的函数,而是执行该函数,并且由于convert()不返回函数,处理程序将不执行任何操作尝试etherUnits.oninput = convert

喵喵时光机

你可以试试这是有效的。HTML:<div id="conversions">&nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; <input type="number" id="etherAmount" value="2" onchange="covert()"> ether to <select id="etherUnits"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oninput="convert()">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option id="wei" value="wei">Wei</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option id="mwei" value="mwei">Mwei/Lovelace/Picoether</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option id="gwei" value="gwei">Gwei/Shannon/Nanoether/Nano</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option id="szabo" value="szabo">Szabo/Microether/Micro</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option id="finney" value="finney">Finney/Milliether/Milli</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option id="ether" value="ether">Ether</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option id="kether" value="kether">Kether/Grand</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option id="mether" value="mether">Mether</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option id="gether" value="gether">Gether</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option id="tether" value="tether">Tether</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" value="Convert" id="convert" onclick="convert()">&nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; </p></div><div id="resultsContainer">&nbsp; &nbsp; <p id="results"></p></div>JS:&nbsp; &nbsp; const converter = require("ethereumjs-units")&nbsp; &nbsp; let etherUnits = document.getElementById("etherUnits")&nbsp; &nbsp; let etherAmount = document.getElementById("etherAmount")&nbsp; &nbsp; let convertButton = document.getElementById("convert")&nbsp; &nbsp; let results = document.getElementById("results")&nbsp; &nbsp; //Takes value of ether input box and converts it&nbsp; &nbsp; function convert() {&nbsp; &nbsp; &nbsp; &nbsp; //value of "convert to" box&nbsp; &nbsp; &nbsp; &nbsp; let etherUnitVal = etherUnits.options[etherUnits.selectedIndex].value&nbsp; &nbsp; &nbsp; &nbsp; results.innerHTML = converter.lazyConvert(etherAmount.value.toString() + " eth", etherUnitVal)&nbsp; &nbsp; }我只是更改并添加了 oninput,onchange 到相应的块而不是在 js 中
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript