How to disable the enter button from triggering the onkeyup event?

In my asp.net solution, I have a text input box and a search button. There is a onkeyup jquery event on the text input field so it automatically clicks the search button when the user presses a key. You can also manually click the button.

But what I noticed is that if you are typing, and then you press ENTER key, it will trigger the on onkeyup event. How can I disable the function from occurring if the ENTER key was pressed, or maybe detect if it was the ENTER key and then have an if statement or something.

Another thing that is happening is, if there is something wrong with the text in the input box, I display an alert message box. Then if you press ENTER to close the box, it will somehow trigger the onkeyup event, which displays the alert box again...

Thanks.


在函数中的所有动作之前添加if (evt.keyCode != 13) :)


You can use .which on the event to determine the KeyCode for the key that was pressed (ENTER = 13):

$('#input').keyup(function(event) {
    if(event.which == 13)
    {
         //respond to enter keypress
    }


});

Also you can use this site to easily find info about keyups/downs etc for different keys.

Hope this helps!


希望这可以帮助。

<script type="text/javascript"> 

function stopRKey(evt) { 
  var evt = (evt) ? evt : ((event) ? event : null); 
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;} 
} 

document.onkeypress = stopRKey; 

</script>
链接地址: http://www.djcxy.com/p/51626.html

上一篇: 绑定onclick输入密钥

下一篇: 如何禁用输入按钮来触发onkeyup事件?