简体   繁体   English

按“输入”键IE7不会阻止默认操作

[英]IE7 on pressing “enter” key does not prevent default action

I have a form which contains 我有一个包含的表格

<button class="submit">Submit</button>

and jQuery code 和jQuery代码

$(".submit").click(function(e){
   var evt = e || window.event; // IE compatibility
   if(evt.preventDefault){  
     evt.preventDefault();  
   }else{  
      evt.returnValue = false;  
    evt.cancelBubble=true;  
  }
 //....ajax call 
});

So, I was wondering what am I doing wrong that in IE7 when I type search term and press enter it goes to my home page(action in form ="/"). 所以,我想知道我在IE7中做错了什么,当我输入搜索词然后按Enter键进入我的主页(action in form =“/”)。 Why it does not prevent IE7 from going to default action. 为什么它不会阻止IE7进入默认操作。

  • is it because i am using button tag 是因为我使用按钮标签
  • i also tried old way in form onsubmit call some function but it fails 我也尝试过旧方式onsubmit调用一些函数,但它失败了
  • or is it because of ajax call? 或者是因为ajax电话?

Additional note: 附加说明:

  • I have body and inside that body I have iframe and in that iframe i have form. 我有身体和身体内部我有iframe和iframe我有形式。 So, I wonder is it because of iframe. 所以,我想知道是因为iframe。

Thanks Viral 谢谢病毒

You are preventing the default action of the button tag, which does not affect input triggered events, also note that jQuery Event object preventDefault() method is cross-browser, try this: 您正在阻止按钮标记的默认操作,这不会影响输入触发的事件,还要注意jQuery Event对象的preventDefault()方法是跨浏览器的,请尝试以下操作:

$('input').on('keyup', function(e){
  if (e.which == 13) {
     e.preventDefault();
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM