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条回答
霸刀☆藐视天下
2楼-- · 2019-01-25 03:09

This works for me in IE8. I had this problem when using only one input field.

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

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-25 03:11

I think everthing is much more complicated than you think...

when a form's display value is set to none with a css class or just with a style attribute on page inital, hitting the enter key in a text field does not work only if you have more than one input field with text type... if you have one text field it works fine.. if you have more than one, it does not fire form submission...

Here i made a demo...

Works Fine (Normal Form) http://yasinergul.com/stackoverflow/ie8-enter-key-bug/one.html

Works Fine (Form hidden & set back visible but it's with one text input) http://yasinergul.com/stackoverflow/ie8-enter-key-bug/two.html

Does Not Work (Form hidden & set back visible but it's with two text input) http://yasinergul.com/stackoverflow/ie8-enter-key-bug/three.html

i think the best approach is to give a .hidden class to the object but not setting display:none for this css selector. you can make it hidden with jquery like

$(".hidden").hide();

as the page loads the form is shown for miliseconds but gets hidden after jquery works...

查看更多
欢心
4楼-- · 2019-01-25 03:11

Found a working solution.

Make the submit button invisible instead of using display:none;

input#submit {
color: transparent;
background: transparent;
border: 0px;
}
查看更多
Fickle 薄情
5楼-- · 2019-01-25 03:18

You may want to add a onkeyup event to your input boxes so that if you hit an enter in the input box then it will also submit.

As CodePartizan mentioned, you need the focus on the button otherwise, so if you tab over to the button, or click on it, it seems to work for me also.

查看更多
该账号已被封号
6楼-- · 2019-01-25 03:20

Old ticket, but I'd like to add what I think is the explanation:

IE8 does the following peculiar thing: the Enter key will submit the form, but any

<input type="submit" name="MySubmitButton" value="I hope I detect THIS VALUE in POST" />

won't be sent in the POST.

IE9 changes the behavior and sends the value. Chrome has always sent the value, as far as my tests have shown.

I hope this helps...

查看更多
可以哭但决不认输i
7楼-- · 2019-01-25 03:20

I believe Yasin has got the point. I just had the same problem: multiple text fields within a form whose visibility is "hidden". My workaround (to prevent the form from flashing) is to set the form opacity to 0 in the css, and then customise its style settings with jQuery on document ready.

I believe this is not something to fix with JS.

查看更多
登录 后发表回答