亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號