onsubmit=“return false” has no effect on Internet

2019-01-19 16:02发布

I have a form that will be submitted by javascript code triggered in "onsubmit" of the tag. Works fine on all browsers - but not on IE7/IE8.

What can I do?

<form action="/dosomething.htm" method="GET" onsubmit="submitmyform();return false">
  [...]
  <input type="submit" value="Go">
</form>

14条回答
三岁会撩人
2楼-- · 2019-01-19 16:13
document.forms.userFormRegisterr.onsubmit =  function(e){
    return false;
};
查看更多
在下西门庆
3楼-- · 2019-01-19 16:14

The solution for us was to move the javascript event from "onsubmit" of the form to "onclick" of the submit button.

<form action="/dosomething.htm" method="GET">
  [...]
  <input type="submit" value="Go" onclick="submitmyform();return false">
</form>
查看更多
叛逆
4楼-- · 2019-01-19 16:15
<script type="text/javascript">
<!--

function submitHandler()
{
  alert(0);
  return false;
}

window.onload=function(){document.getElementById("formid").attachEvent("onsubmit", submitHandler);}
-->
</script>

<form action="/dosomething.htm" method="GET" id="formid">
  [...]
  <input type="submit" value="Go">
</form>

The attach event method only works only for IE7/8. If you want a reliable cross browser solution you should use addEventListener as an alternative.

Hope it helps

查看更多
\"骚年 ilove
5楼-- · 2019-01-19 16:20

Several ideas proposed here work (because they use different ways to write correct code), but there is a much easier answer

OP wrote :

onsubmit="submitmyform();"

instead of :

onsubmit="return submitmyform();"

That's it.

查看更多
戒情不戒烟
6楼-- · 2019-01-19 16:23

I don't think your return false is ever reached, as it comes after what's returned from your function.

<form action="/dosomething.htm" method="GET" onsubmit="submitmyform();return false">

Make sure that you return false inside of your 'submitmyform()' function else, if it's not then it could be returning true to you form obsubmit event.

查看更多
劳资没心,怎么记你
7楼-- · 2019-01-19 16:28

To cancel an event in legacy IE, you need to set returnValue to false. Simply returning false won't do it. I don't know why Microsoft implemented it in this way.

function validateForm(evt) {
//  if form is not valid
    if(!condition) {
    //  if evt has preventDefault
        if(evt.preventDefault)
            { event.preventDefault(); }
    //  if evt has returnValue
        else if(evt.returnValue)
            { evt.returnValue = false; }
    //  fallback
        else
            { return false; }
    }
}

//  assume form is reference to your form
form.attachEvent('onsubmit',validateForm);
查看更多
登录 后发表回答