簡體   English   中英

當我按退格鍵時仍然顯示錯誤消息(e.which ==8)

[英]error message still showing when i press backspace(e.which ==8)

這是代碼。 我只想在按下退格鍵時隱藏消息,我不知道為什么它不會在退格鍵上消失,只是因為它工作正常。

    //on keypressed in textbox
    $(".phone").keypress(function (e) {
        if (e.which == 8 ){
            $(".errmsg").hide();
        }
        //if not numeric, then it don't let you type
        if (e.which != 8 && e.which != 43 && e.which != 45  && (e.which < 48 || e.which > 57)) {
           debugger
            //display error message
            $(".errmsg").html("Digits Only").show();
            e.preventDefault();
        } else {
            debugger
            $(".errmsg").hide();
        }
    });

請使用keyupkeydown事件來處理這個問題。

 //on keypressed in textbox $(".phone").on('keyup keydown', function(e) { if (e.which == 8) { console.log('backspace key pressed'); } //if not numeric, then it don't let you type if (e.which != 8 && e.which != 43 && e.which != 45 && (e.which < 48 || e.which > 57)) { //display error message $(".errmsg").html("Digits Only").show(); e.preventDefault(); } else { $(".errmsg").hide(); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" class="phone" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM