onsubmit=“return false” has no effect on Internet Explorer 7/8 (form is still submitted)

后端 未结 14 1996
耶瑟儿~
耶瑟儿~ 2020-12-11 04:03

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?

<
相关标签:
14条回答
  • 2020-12-11 04:39

    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);
    
    0 讨论(0)
  • 2020-12-11 04:44

    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.

    0 讨论(0)
提交回复
热议问题