Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何暂停提交事件直到AJAX事件完成?_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 如何暂停提交事件直到AJAX事件完成?

Javascript 如何暂停提交事件直到AJAX事件完成?,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一张表格和一份提交单。如果有东西被填满了,在提交之前,我想确保它是一个好的值(使用ajax),如果是这样,那么我只能让它继续。所以 $('#contact').submit(function() { // ajax $.ajax({ type: 'POST', url: $(this).attr('ajaxgetaccommodationsbyemail'), success: function(answer) { here a popup w

我有一张表格和一份提交单。如果有东西被填满了,在提交之前,我想确保它是一个好的值(使用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阻止提交,然后实际调用本机submit来发送表单

$('#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

$('#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
        });
    }
  });
});

如果您确实想要一个可定制的对话框,那么可以使用:

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
    }
  });

});

当AJAX成功返回时,设置一个全局变量。如果未设置全局变量,请取消
submit
事件。但是“if(confirm('Are sure')){”不是这样的。它的另一个“异步”功能不重要,同样的原则也适用,您可以在
success
函数中添加弹出窗口,如果它是自定义弹出窗口,它有一个回调,您可以在其中调用
submit()
,我会将答案编辑回初始状态,但在提交时,默认行为将被禁用。当我调用form.submit()时,它将再次禁用默认行为……不,如果您调用本机提交,则不会