2012-03-22 117 views
0

我有一个包含几个提交按钮,但是我只希望他们中的一个,按ENTER键禁用ENTER键选择在表单中提交按钮

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>"; 

按钮我想actived被激活页面按返回键是submit4

我可以禁用所有的提交按钮回车键,但我竭力要做到这一点选择

回答

3

您可以捕获并取消在这样的领域输入按键:

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

然后与你的类型作为输入提交只给他们一类=“noEnterSubmit” :)

在jQuery中1.4.3可以缩短到这一点:

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

+0

非常感谢您的帮助 – 2012-03-22 21:18:58

1

这是不可能的纯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)”> 

如果你只想在一个表单上禁用它(你必须把它添加到所有的输入中)。

+0

您好,感谢您的输入,但都选择似乎从一个工作停止回车键ll – 2012-03-22 20:52:33

+0

我尝试了两种形式的第二种方法,每种都输入一个文本。第二个有onKeyPress。如预期的那样,我可以在第一次但不是第二次击中entre。 [这是我的测试代码](http://pastebin.com/dHXhWYf2) – paul 2012-03-22 21:00:17