2012-03-22 86 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