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:07

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;
});
查看更多
Explosion°爆炸
3楼-- · 2019-01-19 16:08

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.

查看更多
祖国的老花朵
4楼-- · 2019-01-19 16:08
<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

查看更多
孤傲高冷的网名
5楼-- · 2019-01-19 16:12

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.

查看更多
迷人小祖宗
6楼-- · 2019-01-19 16:13

try this. work for me.

onSubmit="javascript:return false"

查看更多
Luminary・发光体
7楼-- · 2019-01-19 16:13

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.

查看更多
登录 后发表回答