onsubmit=“return false” has no effect on Internet

2019-01-19 16:18发布

问题:

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>

回答1:

I'm going to nitpick this. If you want to handle form submissions, that is what submit is for. If the user hits enter in one of your fields, your onclick handler will be totally avoided. Here is a basic example of doing this in a non-obtrusive way.

<form name="myform">
  <input type="submit" />
</form>
<script>
  document.myform.onsubmit = function(){
    alert('handled');
    return false;
  }
</script>

This can be made a lot simpler with jQuery, same form...

$("form[name=myform]").bind('submit',function(){
   alert('handled');
   return false;
});


回答2:

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.



回答3:

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.



回答4:

<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



回答5:

try this. work for me.

onSubmit="javascript:return false"



回答6:

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);


回答7:

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>


回答8:

I had the same error and when I turned on the console, it never came. So I figured out, it was the console, that wasnt known by the internet explorer as long as it is turned off. Simply add a dummy console to the window with the following code (if there isnt already one):

var myconsole = {log : function(param){}}
window.console = window.console || myconsole;

With this you could get in trouble, if you want to have microsofts javascript console. Then just comment the lines out. But now, the console object is not undefined anymore on the whole site.



回答9:

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

this works fine... the function must return true or false



回答10:

In fact, because you write submitmyform();return false only submitmyform is evaluated.

In the case of two commands, you will have to put them between braces like this

{submitmyform();return false;}

so that both are evaluated.



回答11:

I think that the problem is in return false you need to return it from your 'onsubmit' function

see example.

This form will never be submitted in IE 7/8 as well.

 <form action="acknowledgement.html" method="get" onsubmit="verify();">
        <input type="submit" name="submit"
         VALUE="Submit"> 
        </form>

<script language="javscript">
function verify(){

return false;
}
</script>

Danny.



回答12:

It works - check W3Scools example (taken from ) http://www.w3schools.com/js/js_form_validation.asp

<html>
<head>
<script type="text/javascript">
function validate_required(field,alerttxt)
{
with (field)
  {
  if (value==null||value=="")
    {
    alert(alerttxt);return false;
    }
  else
    {
    return true;
    }
  }
}

function validate_form(thisform)
{
with (thisform)
  {
  if (validate_required(email,"Email must be filled out!")==false)
  {email.focus();return false;}
  }
}
</script>
</head>

<body>
<form action="submit.htm" onsubmit="return validate_form(this)" method="post">
Email: <input type="text" name="email" size="30">
<input type="submit" value="Submit">
</form>
</body>

</html>


回答13:

If you want IE to be standards compliant, you'll have to tell it which standard it's supposed to comply with in a declaration.

Without one it's going to be endless frustration to get it to do what you want. With one, most of the frustration goes away because it starts working as every other browser.



回答14:

document.forms.userFormRegisterr.onsubmit =  function(e){
    return false;
};