繁体   English   中英

禁用表单中选定提交按钮的回车键

[英]Disabling enter key for selected submit buttons within a form

我有一个包含几个提交按钮的页面,但是我只希望其中一个通过回车键激活

 echo "<form method=\"post\" action=\"fish.php\">\n";

 echo "<tr><td>North Pot</td><td><input type=\"text\" name=\"northpot\"><input type=\"submit\" name=\"north_all\" value=\"All\"></td>\n";

 echo "<tr><td>South Pot</td><td><input type=\"text\" name=\"southpot\"><input type=\"submit\" name=\"south_all\" value=\"All\"></td>\n";

  echo "<tr><td><input type=\"submit\" name=\"submit4\" value=\"Place\"><input type=\"submit\" name=\"clear\" value=\"Clear\"></td>\n";
 echo "<td colspan=\"2\" align=\"center\"></td></tr>\n";

 echo "</form>"; 

我想通过返回键激活的按钮是submit4

我可以为所有提交按钮禁用回车键,但是我正在努力地有选择地这样做

您可以在以下字段上捕获并取消Enter键:

$('.noEnterSubmit').keypress(function(e){
    if ( e.which == 13 ) return false;
    //or...
    if ( e.which == 13 ) e.preventDefault();
 }); 

然后在您输入的类型为Submit的情况下,给他们一个class =“ noEnterSubmit” :)

在jQuery 1.4.3中,您可以将其缩短为:

$('.noEnterSubmit').bind('keypress', false);

在纯HTML中这是不可能的。 您怎么可能用Javascript做到这一点:

function keyPressed(e)
{
     var key;      
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox      

     return (key != 13);
}

然后在您的页面中,添加

<body onKeyPress="return keyPressed(event)">

如果要在孔页面上禁用输入,或者

<input type=”text” onKeyPress=”return keyPressed(event)”>

如果您只想在单个表单上将其禁用(然后必须将其添加到所有输入中)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM