IE8 - Form within a hidden div, return key no longer works

这一生的挚爱 提交于 2019-12-11 01:15:44

问题


I have a login form that resides within a div with display: none; I then fade in the form using jquery but the Enter/Return key no longer submits the form. If I change the display to block, it works fine. This problem is IE only.

Any ideas?

Here's an example code that doesn't work;

<div id='formdiv' style='display: none;'>
<form id='loginForm' name='createLoginForm' method='post' action='' >
    <input type='text' name='username' id='username' />
    <input type='password' name='password' id='password' />
    <input type='submit' name='submit' id='login_submit'  />
</form>
</div>

And I stuck this in the to test the hiding/showing;

<script type="text/javascript" src="jquery.tools.min.js"></script>

<script type='text/javascript'>
setTimeout('$("#formdiv").show()',1000);
</script>

回答1:


Since the problem seems relevant to whether the element appears on the page or not, you can still have it appear, but "cloak" it using a 1x1 wrapper div with overflow: hidden;

<style type="text/css" media="screen">
  .submit-wrapper
  {
    overflow: hidden;
    height: 1px;
    width: 1px;
    position: absolute;
    top: 0;
    right: 0;
  }
</style>

<div class="submit-wrapper">
  <input type="submit" value="Save" />
</div>



回答2:


Not sure what's causing the problem, but refreshing the div's html after it's shown seems to fix the form:

$(document).ready(function(){
    $('#formdiv').show(500, function(){
        $('#formdiv').html($('#formdiv').html());
    });
});

Definitely not ideal, but since it seems to be an actual IE8 or jQuery bug, you may have to resort to a hack like this (maybe with a browser check around it).




回答3:


I've been bitten by this before. Basically, if the form was hidden when the page is rendered, then return-submit won't work for that form.

Here's a work around: http://www.thefutureoftheweb.com/blog/submit-a-form-in-ie-with-enter



来源:https://stackoverflow.com/questions/1795985/ie8-form-within-a-hidden-div-return-key-no-longer-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!