开发者

Getting the OnClick event when enter is pressed (IE)

I have the following code (inherited from someone):

<div class="feedback">&nbsp;</div>
   <form onsubmit="return false" autocomplete="off">
      Enter your guess: 
      <input type="text" size="48" id="guessedword"/>
      <input type="submit" value="Guess!" id="guessbutton"/>
   </form>
</div>

Now, I want to do something when the "Guess!" button is pressed, but I don't want the page to change (i.e. I don't want the form submitted). That's why the onsubmit is mapped to "return false". I also hav开发者_开发技巧e the following JQuery:

$("#guessbutton").bind("click", onGuess);

The thing is, this works great in Firefox and Chrome. The only problem is IE. The guess button works correctly, the only problem is that when pressing Enter in the text field, the button isn't pressed, or at least the onClick behavior is never called. In Firefox and Chrome, it works great.

Is there any way to make this work the way I want it? Or am I going about this whole thing the wrong way? (I'm new to HTML, so I'm not even sure this is the right way to do things).

Thanks


you can write :

<form onsubmit="onGuesS(); return false;" autocomplete="off">

This will not redirect enter to onclick, but in all the three browsers, Enter is equivalent to submitting the form.. so u write ur login in onSubmit and the return false that the page does not get submitted again.


Try to use onkeypress() event and handle enter key press. Dont use type="submit". Use type="button" instead. If you dont submit form to the server dont use it at all. Remove form tag from code.

<div class="feedback">&nbsp;</div>
      Enter your guess: 
      <input type="text" size="48" id="guessedword"/>
      <input type="button" value="Guess!" id="guessbutton"/>
</div>

document.onkeypress = function(){
   //your handling code here. keyKode of Enter key = 13
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜