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

后端 未结 14 1947
耶瑟儿~
耶瑟儿~ 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:19

    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.

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

    0 讨论(0)
  • 2020-12-11 04:22

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

    try this. work for me.

    onSubmit="javascript:return false"

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

    0 讨论(0)
  • 2020-12-11 04:28

    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>
    
    0 讨论(0)
提交回复
热议问题