如何使用 jQuery 檢測鍵盤上按下回車鍵?
要檢測鍵盤上按下回車鍵,請使用包含 keyCode 的 keyup() 函式。你可以嘗試執行以下程式碼以瞭解如何使用 jQuery 檢測鍵盤上按下回車鍵 -
示例
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $('textarea').bind("enterKey",function(e){ alert("You have pressed Enter key!"); }); $('textarea').keyup(function(e){ if(e.keyCode == 13) { $(this).trigger("enterKey"); } }); }); </script> </head> <body> <p>Press Enter below</p> <textarea rows="2" cols="20"> </textarea> </body> </html>
按下以下回車鍵
廣告