[jQuery]keydownでキー入力を無効化するサンプル

2021年6月30日

書式
$(“#セレクターID").keydown(function(event)
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<input id="usrId" type="text" value="" maxlength="1" autocomplete="off">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$("#usrId").keydown(function(event) {
if (event.key === "Backspace"){
console.log("Backspace key is pressed");
return false;
}
});
</script>
<input id="usrId" type="text" value="" maxlength="1" autocomplete="off"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $("#usrId").keydown(function(event) { if (event.key === "Backspace"){ console.log("Backspace key is pressed"); return false; } }); </script>
<input id="usrId" type="text" value="" maxlength="1" autocomplete="off">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$("#usrId").keydown(function(event) {
      if (event.key === "Backspace"){
            console.log("Backspace key is pressed");
            return false;
      }
});
</script>

 

jQuery

Posted by arkgame