为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

陪伴而非守候 2021-11-12 15:00:03
我正在构建一个简单的应用程序,可将 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 itfunction convert() {    //value of "convert to" box    let etherUnitVal = etherUnits.options[etherUnits.selectedIndex].value    results.innerHTML = converter.lazyConvert(etherAmount.value.toString() + " eth", etherUnitVal)}
查看完整描述

2 回答

?
慕容森

TA贡献1853条经验 获得超18个赞

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

尝试

etherUnits.oninput = convert


查看完整回答
反对 回复 2021-11-12
?
喵喵时光机

TA贡献1846条经验 获得超7个赞

你可以试试这是有效的。


HTML:


<div id="conversions">

    <p>

        <input type="number" id="etherAmount" value="2" onchange="covert()"> ether to <select id="etherUnits"

            oninput="convert()">

            <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" onclick="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")


    //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)

    }

我只是更改并添加了 oninput,onchange 到相应的块而不是在 js 中


查看完整回答
反对 回复 2021-11-12
  • 2 回答
  • 0 关注
  • 162 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信