admin管理员组文章数量:1430940
I have an ajax function for saving a forms data. I want it to remain asynchronous because the users can hit save any time. However, I have another function that converts the form to a PDF and I want it to run the save function before creating the PDF (in case the users have added more data). Is there a way to make $('input.submit')
wait for save to finish before opening the pdf? Below is the jQuery I am using:
$("button#save").on('click', function (){
$.ajax({
type: 'POST',
url: '<?php echo matry::base_to('utilities/crm/field_day_save');?>',
data: $("form#trip_form").serialize(),
dataType: 'json',
success: function (data)
{
$("#alerts").html(data.alert);
$("#form_id").val(data.id);
}
});
});
$("input.submit").on('click', function(event){
event.preventDefault();
$("button#save").trigger('click');
window.open('<?php echo matry::base_to('custom_worddocs/field_day');?>' + '&fd_id=' + $("#form_id").val());
});
In short, I want $('button#save').click()
to remain asynchronous, but I want $(input.submit)
to wait for button save to plete before opening new window.
I have an ajax function for saving a forms data. I want it to remain asynchronous because the users can hit save any time. However, I have another function that converts the form to a PDF and I want it to run the save function before creating the PDF (in case the users have added more data). Is there a way to make $('input.submit')
wait for save to finish before opening the pdf? Below is the jQuery I am using:
$("button#save").on('click', function (){
$.ajax({
type: 'POST',
url: '<?php echo matry::base_to('utilities/crm/field_day_save');?>',
data: $("form#trip_form").serialize(),
dataType: 'json',
success: function (data)
{
$("#alerts").html(data.alert);
$("#form_id").val(data.id);
}
});
});
$("input.submit").on('click', function(event){
event.preventDefault();
$("button#save").trigger('click');
window.open('<?php echo matry::base_to('custom_worddocs/field_day');?>' + '&fd_id=' + $("#form_id").val());
});
In short, I want $('button#save').click()
to remain asynchronous, but I want $(input.submit)
to wait for button save to plete before opening new window.
-
You open the pdf in the
success
orplete
callbacks of the ajax request. – Danny Commented Feb 15, 2013 at 18:53 - Why are you triggering the click again? – Joseph Silber Commented Feb 15, 2013 at 18:58
- The click is to run the ajax save function, so it saves before creating the pdf. @Danny, I don't want to have to rewrite the save function with just an additional line for opening the window, and I don't want the window to open every time its saved – Mike Commented Feb 15, 2013 at 19:05
2 Answers
Reset to default 4jQuery's ajax
function returns a jqXHR
object which, among other things, behaves like a deferred.
By only calling window.open
from within the then
function, it'll wait for the AJAX to plete:
$("button#save").on('click', function () {
var jqXHR = $.ajax({ /* your config... */ });
$("input.submit").one('click', function(event) {
event.preventDefault();
$("button#save").trigger('click');
jqXHR.then(function () {
window.open('<?php echo matry::base_to('custom_worddocs/field_day');?>' + '&fd_id=' + $("#form_id").val());
});
});
}
Have your click handler return a promise object, then use triggerHandler()
to trigger the click event and get it's return value.
$("button#save").on('click', function (){
return $.ajax({
...
and
...
$("button#save").triggerHandler('click').done(function(){
window.open(...);
});
...
Proof of concept: http://jsfiddle/SRzcy/
本文标签: javascriptJquery Wait Until AJAX Call is DoneStack Overflow
版权声明:本文标题:javascript - Jquery Wait Until AJAX Call is Done - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745562565a2663559.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论