In IE8 enter key in a form does not work

2019-01-25 02:41发布

I have a problem that in IE8 the enter does not work to submit a form. I have generated a test page to expose this problem. It seems that displaying the form in the onLoad function disables results that the enter button does not trigger a submit anymore. Is this a bug in IE8 or is it some security issue?

The code to reproduce this is:

onload = function() { 
    document.getElementById('test').style.display = 'block'; 
} 
#test {
    display: none;
}
<form id="test" method="get" action="javascript:alert('woei!')"> 
    <input type="text" name="user" value=""> 
    <input type="password" name="pw" value="">
    <input type="submit" value="submit" id="submit"> 
</form> 

14条回答
Deceive 欺骗
2楼-- · 2019-01-25 03:27

I had the same issue with ie and none of the solutions helped until I read this:

http://www.rachaelarnold.com/dev/archive/enter-key-form-submission-bug#ixzz2Y5Zwgj2k

my form only had one input field....duh! :)

查看更多
三岁会撩人
3楼-- · 2019-01-25 03:29

A fix to what @Jitendra Pancholi suggested- now it submits only the form we want, not all of them

$("form input").keypress(function (e) {
   if(e.which === 13) {
      $(this.form).submit();
   }
});
查看更多
登录 后发表回答