How do I redirect users after submit button click?

后端 未结 7 2307
悲哀的现实
悲哀的现实 2020-12-10 04:55

How do I redirect users after submit button click? My javascript isn\'t working:

Javascript



        
7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-10 05:25

    Your submission will cancel the redirect or vice versa.

    I do not see the reason for the redirect in the first place since why do you have an order form that does nothing.

    That said, here is how to do it. Firstly NEVER put code on the submit button but do it in the onsubmit, secondly return false to stop the submission

    NOTE This code will IGNORE the action and ONLY execute the script due to the return false/preventDefault

    function redirect() {
      window.location.replace("login.php");
      return false;
    }
    

    using

    Or unobtrusively:

    window.onload=function() {
      document.getElementById("form1").onsubmit=function() {
        window.location.replace("login.php");
        return false;
      }
    }
    

    using

    jQuery:

    $("#form1").on("submit",function(e) {
       e.preventDefault(); // cancel submission
       window.location.replace("login.php");
    });
    

    -----

    Example:

    $("#form1").on("submit", function(e) {
      e.preventDefault(); // cancel submission
      alert("this could redirect to login.php"); 
    });
    
    
    
    

提交回复
热议问题