简体   繁体   中英

jquery + ajax - redirect after submit form in new tab with no button

I want to submit my form in new tab and after the submit redirect to another page. (response not required)

HTML

<head>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>   

</head>


<form  action="my url" name="myForm" id="myForm"  target="_blank">
-------
------
</form>

jQuery

    <script type="text/javascript"> 

    $(function() {
      $.ajax({  
          type: $('#myForm').attr('method'),  
          url: $('#myForm').attr('action'),  
          data: $('#myForm').serialize(),       
          success: function(){  
              alert("success"); 
          //redirect to another page
          }  
      });
    });


</script>

I don't have any buttons. There are many examples, but none of them works for me.

UPDATE: The problem is that my form doesn't submit. The form url I am using to submit the data, "my url" is a payment page url.

$(function() {
$.ajax({  
  type: $('#myForm').attr('method'),  
  url: $('#myForm').attr('action'),  
  data: $('#myForm').serialize(),       
  success: function(){  
      alert("success"); 
  //redirect to another page
window.open(
'www.google.com',
'_blank' // <- This is what makes it open in a new tab.
);
}  
});
});

If success doesn't work for you, try with error

$.ajax({  
  type: $('#myForm').attr('method'),  
  url: $('#myForm').attr('action'),  
  data: $('#myForm').serialize(),       
  success: function(){  
      alert("success"); 
  }
  error: function(e){
      console.log(e);
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM