如何在 HTML 中按下輸入文字中的 ENTER 後觸發行為?


配合 jQuery 使用 HTML 實現此目的,並按下 ENTER 後觸發 −

示例

<!DOCTYPE html>
<html>
   <head>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script>
         $(document).ready(function(){
            $('input').bind("enterKey",function(e){
               alert("Enter key pressed");
            });
            $('input').keyup(function(e){
               if(e.keyCode == 13)
               {
                  $(this).trigger("enterKey");
               }
            });
         });
      </script>
   </head>
 
   <body>
      <input type = "text">
      <p>Press Enter key in the above input text.</p>
   </body>
</html>

更新於: 2020-06-25

362 次瀏覽

開啟您的 職業生涯

透過完成課程取得認證

開始學習
廣告
© . All rights reserved.