This question already has answers here:
Prevent users from submitting a form by hitting Enter

(32个答案)


4年前关闭。




我只想禁用键盘上的Enter键。以下脚本出于某种原因将整个键盘锁定,除了仍然只允许使用Enter键外。

如果这有助于查明缺失或错误的地方,那么我正在使用V.S。 2005,VB.NET 2.0和I.E. 7。
<meta http-equiv="content-type" content="text/html; charset=windows-1252">

<head>
    <meta http-equiv="content-type" content="text/html; charset=windows-1252">

    <script language="JavaScript">
    function TriggeredKey(e)
    {
        var keycode;
        if (window.event) keycode = window.event.keyCode;
        if (window.event.keyCode = 13 ) return false;
    }
    </script>
</head>
<body onkeydown="TriggeredKey(this)">

最佳答案

如果您有jQuery,请尝试以下操作:

$('html').bind('keypress', function(e)
{
   if(e.keyCode == 13)
   {
      return false;
   }
});

关于javascript - 禁用键盘<enter>键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1235716/

10-12 00:17
查看更多