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

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

如何在javascript中讀取多個值長度?

如何在javascript中讀取多個值長度?

白板的微信 2023-06-09 17:27:52
我正在使用條形碼掃描儀,目前遇到一個問題,我需要條形碼讀取 2 個不同的值長度,目前我將其設置為以 9 長度提交值。          <span>                <input type="text" id="IC-input" name="IC" onkeyup="autofill(this.value)" placeholder="Enter your IC Number" required maxlength="12">                <label><button type="button" id="theButton" onclick="theButtonIsPressed()">Submit</button></label>          </span>        function autofill(value){        console.log("Autofill:"+value)        //console.log(9 digits);        button = document.getElementById("theButton");        if(value.length == 9){            console.log('form is ready to submit');            theButtonIsPressed(value);        }     }現在我也需要它從 12 個值讀取,但是當該值達到 9 位時它會自動提交。我試過 OR 功能。        function autofill(value){        console.log("Autofill:"+value)        //console.log(9 digits);        button = document.getElementById("theButton");        if(value.length == 12 || value.length == 9){            console.log('form is ready to submit');            theButtonIsPressed(value);        }     }我也試過 Else 函數        function autofill(value){        console.log("Autofill:"+value)        //console.log(9 digits);        button = document.getElementById("theButton");        if(value.length == 12){            console.log('form is ready to submit');            theButtonIsPressed(value);        }         else if(value.length == 9){            theButtonIsPressed(value);        }    }但它總是會讀取前 9 個值,而不會讀取其他 3 個值。有人對此有解決方案嗎?先感謝您。
查看完整描述

3 回答

?
catspeake

TA貢獻1111條經驗 獲得超0個贊

好像你在聽按鍵。使用計時器取消它。去抖動方法的基本思想。


var timer;

function autofill(value){

  if (timer) window.clearTimeout(timer);

  if(value.length === 9){

    timer = window.setTimeout( function () {

      processIt(value);

    }, 50);

  } else if(value.length === 12){

    processIt(value);

  } 

}


function processIt(value){

  console.log('here', value);

}

但這是一個糟糕的解決方案。通常,您將掃描儀設置為觸發標簽或輸入 press,這樣您就知道它已完成。我會檢查是否發生這種情況,然后改為傾聽。然后你可以只聽那個,你就知道掃描儀已經完成了。


var inp = document.getElementById("barcode");

inp.addEventListener("keydown", function (evt) {

  if (["Tab", "Enter"].includes(evt.key)) {

    evt.preventDefault();

    console.log('scanner is done', evt.target.value);

  }

});

<input type="text" id="barcode" />


查看完整回答
反對 回復 2023-06-09
?
森欄

TA貢獻1810條經驗 獲得超5個贊

對于最常見的場景,掃描儀會一個一個觸發這樣的事件:焦點,輸入字符....輸入最后的'Enter'字符,所以你必須關注最后一個事件。


<script type="text/javascript">

    window.addEventListener("load", function () {

        var ic = document.getElementById("IC-input");

        ic.addEventListener("focus", function (args) {

            ic.value = "";

        });

        ic.addEventListener("keyup", function (args) {

            if (args.key == "Enter") {

                autofill(ic.value);

            }

        });

    });

    function autofill(value) {

        console.log("Autofill:" + value)

        //console.log(9 digits);

        button = document.getElementById("theButton");

        if (value.length == 9) {

            console.log('form is ready to submit');

            theButtonIsPressed(value);

        }

    }

</script>

<span>

    <input type="text" id="IC-input" name="IC" onkeyup="input_keyup" placeholder="Enter your IC Number" required maxlength="12">

    <label><button type="button" id="theButton" onclick="theButtonIsPressed()">Submit</button></label>

</span>


查看完整回答
反對 回復 2023-06-09
?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

問題是一旦輸入框有 9 個字符,自動填充功能就會運行以按下按鈕。這是因為您正在通過附加到輸入標簽的“onkeyup”事件偵聽器運行自動填充功能。

解決方案是在確保有預期的全長值后運行自動填充功能。祝你好運。


查看完整回答
反對 回復 2023-06-09
  • 3 回答
  • 0 關注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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