Delay page redirect after form submit using jquery / js? [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Clo开发者_如何学运维sed 9 years ago.
Improve this questionMy code is:
<input class="toBasketButton" type="submit" value="">
Something like this?
$(function(){
$('input.toBasketButton').click(function(event){
event.preventDefault();
var button = this;
window.setTimeout(function(form){
$(form).submit();
}, 1000, $(button).closest('form'));
});
});
<form action="" method="post">
<input class="toBasketButton" type="submit" value="Submit">
</form>
(Working example)
You could try using jQuery's ajaxForm plugin ( http://jquery.malsup.com/form/ )
This enables you to submit the form through ajax, which means you never leave the page until you want to. This way, you can have the user submit, perform whatever you need to do, once once you're done redirect via JS or through PHP as usual. The user won't know the difference - the delay will look the same to them, but you gain all the control inbetween the submit click, and the redirection, allowing you to perform whatever else you need.
Example:
$(form).ajaxForm({
dataType: "json",
success: myFormSubmissionIsDone,
url: "/some/controller/action/",
type: "post", timeout: 30000
});
function myFormSubmissionIsDone() {
// This and that, whatever you need to do...
window.location = "/newlocation/redirecting/now/yay";
}
精彩评论