开发者

How Can I Prevent the Enter keypress from Triggering a Button Click?

开发者 https://www.devze.com 2023-03-20 03:20 出处:网络
I have the following code snippet. $(\"#days\").key开发者_StackOverflowpress(function(e){ if(e.keyCode == 13){

I have the following code snippet.

  $("#days").key开发者_StackOverflowpress(function(e){
      if(e.keyCode == 13){
         self.validateNightModalForm();
      }
  });

basically, it checks for enter then invoke a function.

My only problem is that IE8 also clicks a button. When I hit enter. I've checked it with all other browsers, but only IE8 does this.

Is there any way to prevent it?


Try adding those two lines:

if (e.keyCode == 13) {
    self.validateNightModalForm();
    e.preventDefault()
    return false;
}

Hopefully this will stop the keypress event from "going up" and trigger the form submit.

0

精彩评论

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