如何使用 HTML 在文字輸入框按下回車後觸發操作?


使用 HTML 和 jQuery 實現此功能,並在按下回車後觸發 −

範例

<!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 年 6 月 25 日

362 次瀏覽

開啟您的 職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.