如何在AJAX事件结束之前暂停提交事件?

Joh*_*ith 3 javascript ajax jquery

我有一份表格和一份提交.如果有东西被填满,在提交之前我想确保它的价值很高(用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

});
Run Code Online (Sandbox Code Playgroud)

ade*_*neo 6

您可以使用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();
                 }

             });
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

async:false删除,因为你应该永远不会做syncronous阿贾克斯.

  • 无所谓,同样的原则适用,你在`success`函数中添加弹出窗口,如果是自定义弹出窗口,它有一个回调,你可以调用`submit()`,我会编辑回答到最初的方式 (2认同)