最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Disabling enter key for selected submit buttons within a form - Stack Overflow

programmeradmin1浏览0评论

i have a page that contains several submit buttons however i only want one of them to be activated by the enter key

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

The button i want to be actived by the return key is submit4

i can disable the enter key for all the submit buttons but i'm struggling to do this selectively

i have a page that contains several submit buttons however i only want one of them to be activated by the enter key

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

The button i want to be actived by the return key is submit4

i can disable the enter key for all the submit buttons but i'm struggling to do this selectively

Share Improve this question edited Mar 22, 2012 at 20:39 Josh 8,2115 gold badges44 silver badges41 bronze badges asked Mar 22, 2012 at 20:34 connor.pconnor.p 87610 gold badges19 silver badges30 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You can capture and cancel the enter keypress on those fields like this:

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

Then on your inputs with type as submit just give them a class="noEnterSubmit" :)

In jQuery 1.4.3 you can shorten it to this:

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

This is not possible in plain HTML. How ever you may do it in Javascript :

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

     return (key != 13);
}

And then in your page, either add

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

if you want to disable enter on the hole page, or

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

if you only want to disable it on a single form (You must then add this to all your inputs).

发布评论

评论列表(0)

  1. 暂无评论