我有一个表单和一个提交。如果有东西被填满,在提交之前我想确保它是一个好的值(使用 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。
关于javascript - 如何暂停提交事件直到 AJAX 事件完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35073151/