[英]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();
}
});
請使用keyup
和keydown
事件來處理這個問題。
//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.