Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 907
Dirk Paessler Avatar asked Nov 02 '10 13:11

Dirk Paessler


People also ask

What happens when Onsubmit returns false?

It means that do nothing on submit.

What does Onsubmit in form do?

The onsubmit property of a Form object specifies an event handler function that is invoked when the user submits a form by clicking on a Submit button in the form. Note that this event handler is not invoked when the Form.


2 Answers

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;
});
like image 64
Drew Avatar answered Nov 03 '22 01:11

Drew


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.

like image 32
Thibault Witzig Avatar answered Nov 03 '22 01:11

Thibault Witzig