onSubmit returning false is not working

2019-01-14 22:47发布

I'm completely confused ... I'd swear this was working yesterday ... I woke up this morning and all my forms stopped to work in my project.

All the forms have a "onsubmit" to a function that returns false since it's an ajax call, so the form is never sent.

After a lot of tests, I simplified the question to this piece of code:

<html>
<head>
<script type="text/javascript">
 function sub()
 {
  alert ("MIC!");
  return false;
 }
</script> 
</head>
<body>

<form method = "post" id = "form1" onsubmit = "return sub()">
 input: <input type="text" name="input1" >
    <a href="#" onClick="document.getElementById('form1').submit();">button</a> 
</form>

</body>
</html>

I would swear that this works perfectly, but today is nor working :D

Why if I press the button the form is sent ?

I know it's a total newbie question, but I'm stuck

Thank you !

10条回答
smile是对你的礼貌
2楼-- · 2019-01-14 23:21

I've got the same issue (I was using IE 8),

following code is NOT working:

<form method="post" id="form1" onsubmit="sub">
<form method="post" id="form1" onsubmit="sub()">
<form method="post" id="form1" onsubmit="return sub()">

and following code is WORKING,

<form method="post" id="form1" onsubmit="return(sub())">

I just follow the sample here

And please note that:

The submit method does not invoke the onsubmit event handler.

Submit a form using the INPUT TYPE=submit, INPUT TYPE=image, or BUTTON TYPE=submit object.

查看更多
迷人小祖宗
3楼-- · 2019-01-14 23:25

The onsubmit event only fires for normal form submissions. It does not fire when the JavaScript submit() method is called.

The best solution is to use a normal submit button here.

If you want to continue with using a link to the top of the page (which isn't unobtrusive, progressive, or graceful) then you need to call the sub() function explicitly and test its return value before deciding if you should call submit() or not.

查看更多
女痞
4楼-- · 2019-01-14 23:25

It seems that the 'submit()' function bypasses the 'form' tags 'onsubmit' event in Firefox and IE8. If you use a submit button, then it works as expected:

<html>
<head>
<script type="text/javascript">
 function sub()
 {
  alert ("MIC!");
  return false;
 }
</script> 
</head>
<body>

<form method = "post" action="http://google.com" id = "form1" onsubmit = "return sub();">
 input: <input type="text" name="input1" >
     <input type="submit">
</form>

</body>
</html>
查看更多
一夜七次
5楼-- · 2019-01-14 23:25

You could use jquery preventDefault method.

$(function () {
        $('#repForm').bind('submit', function (e) {
            var isValid = true;
            if (btnname == 'test') {
                isValid = ValidateEmailID();
            }
            else {
                isValid = ValidateSomething();
            }

            if (!isValid) {
                e.preventDefault();
            }
        });
    });
查看更多
唯我独甜
6楼-- · 2019-01-14 23:30

Try to change you "button" code like that

<a href="#" onClick="return document.getElementById('form1').onsubmit();">button</a> 
查看更多
看我几分像从前
7楼-- · 2019-01-14 23:35
<head>
  <script type="text/javascript">
   function sub(e)
   {
     e.preventDefault();
     alert ("MIC!");
     return false;
   }
  </script> 
 </head>
 <body>
   <form method = "post" id = "form1" onsubmit = "return sub(event);">
   </form>
查看更多
登录 后发表回答