如何在 AJAX 事件完成之前暂停提交事件
How to pause a submit event until an AJAX event finishes?
我有一个表格和一个提交。如果某些内容已填充,在提交之前,我想确保它具有良好的价值(使用 ajax),如果是这样,那么我只能让它继续。所以
$('#contact').submit(function() {
// ajax
$.ajax({
type: 'POST',
url: $(this).attr('ajaxgetaccommodationsbyemail'),
success: function(answer) {
here a popup window opens, which waits until I press YES or NO
},
async: false
});
this is when return TRUE or FALSE
there should be something like
do while (popupWindowClosed)
so $.when is not an option
});
你可以用jQuery阻止提交,然后实际调用本机提交来发送表单
$('#contact').on('submit', function(e) {
e.preventDefault();
var form = this;
$.ajax({
type: 'POST',
url: $(this).attr('ajaxgetaccommodationsbyemail'),
success: function(answer) {
popup(function(yesorno) {
if (yesorno) {
form.submit();
}
});
}
});
});
并且删除了async:false
,因为您永远不应该执行同步 ajax。
jQuery ajax 有错误和成功回调。您可以执行以下操作:
$('#contact').submit(function() {
// ajax
$.ajax({
type: 'POST',
url: $(this).attr('ajaxgetaccommodationsbyemail'),
error: function (request, error) {
// there was error. handle it how you want
},
success: function () {
// open your pop up window
// and do other stuff
}
});
});
$('#contact').submit(function(e) {
e.preventDefault();
var form = $(this);
// ajax
$.ajax({
type: 'POST',
url: $(this).attr('ajaxgetaccommodationsbyemail'),
success: function(answer) {
$.dialog(function(){ // this line is "pseudocode", put the code of your dialog here.
// when clicked YES
form.submit();
}, function(){
// when clicked NO
});
}
});
});
如果您确实想要一个可自定义的对话框,您可以使用: https://jqueryui.com/dialog/#modal-confirmation
相关文章:
- NodeJS-readline暂停和恢复事件发射器(逐行读取)
- Chrome javascript调试器在超时时暂停,可以't调试单击事件
- 如何在网站上的嵌入式黑莓媒体播放器上跟踪播放和暂停事件
- 在点击和加载事件时播放暂停背景音频
- JS单击事件不适用于暂停CSS动画
- 在单击时暂停并播放 JavaScript 滑块事件
- 播放,Soundcloud oembed 小部件的暂停事件
- HTML5 嵌入代码 - 事件属性未触发(已触发、暂停等)
- 暂停,直到事件/函数使用 jquery 或 JavaScript 完成
- 如何在 AJAX 事件完成之前暂停提交事件
- Froogaloop的“完成”事件也会触发一个“暂停”事件
- 如果鼠标移动到图像上,我如何添加鼠标事件,它将显示播放/停止/暂停按钮
- 在窗口大小调整事件开始时暂停 Greensock 动画,在调整大小事件结束时恢复动画
- 用鼠标播放/暂停gif事件
- 如何解决“;从本机“异常触发恢复/暂停事件”;Cordova安卓系统
- Phonegap/Cordova暂停事件在android的简历上触发;因此无法关闭音频
- 函数运行时的暂停事件
- Cordova:暂停事件而不启动
- 如果使用Knockout注册了暂停事件,为什么无法暂停音频
- 使用OnClick事件暂停/停止视频