简体   繁体   中英

How Do I Get Chrome To Work When Using $('#myForm').submit() Followed By window.location.href =

In IE and FireFox, when I run this:

$('#editForm').submit();

window.location.href = "my url here";

It works. In Chrome, both lines work on their own, but running them together means that the form is not submitted. Even sticking the submit call inside an If statement with the redirect called upon success of this If statement means that it redirects without submitting.

You could rewrite it like so:

$('#editForm').submit(function(){
    window.open('url', 'myWindow', 'target=_blank');
});

IMHO, a more elegant solution would be:

$('#editForm').submit(function(e){
    // Prevent the form from actually submiting
    e.preventDefault();
    // Code to AJAX submit (and Validate) the form goes here
    DoSomethingAjax();
    // Open a new window
    window.open('url', 'myWindow', 'target=_blank');

});

CodePen for the first http://codepen.io/anon/pen/xgwqrE

PS

A clean and elegant solution if you would like only to redirect, would be to use backend code to redirect after the form has been submitted successfully.

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