2017-08-06 84 views
2

我需要在bootbox确认模式上按确认后运行ajax功能。模式短暂闪烁,然后php代码运行而不用等待。我如何让它等待。等待Bootbox确认运行Ajax

我的JavaScript运行时我删除bootbox但我想确认它的运行

我的代码之前,

$('#change_pass_form').submit(function() { 
    bootbox.confirm({ 
     message: "Your password is about to be changed. Are you sure?", 
     buttons: { 
      cancel: {label: '<i class="fa fa-times"></i> Cancel'}, 
      confirm: {label: '<i class="fa fa-check"></i> Confirm'} 
     }, 
     callback: function (result) { 
      if (result === 'true') { 
       $.ajax({ 
        type: 'POST', 
        url: 'php_files/profile_php_files/profile_password_change_process.php', 
        data: { 
         user_id: sessionStorage.getItem('user_id'), 
         new_password: $('#new_password').val(), 
         repeat_password: $('#repeat_password').val() 
        }, 
        success: function (data) { 
         if (data === 'correct') { 
          bootbox.alert({ 
           size: 'small', 
           message: 'Your password has been changed. You will now be logged out.', 
           callback: function() { 
            window.location.replace('index.html'); 
           } 
          }); 
          return true; 
         } else { 
          bootbox.alert({ 
           size: 'small', 
           message: data 
          }); 
          return false; 
         } 
        } 
       }); 
      } else { 
       return false; 
      } 
     } 
    }); 
}); 

回答

1

所有return falsereturn true正在返回的回调,而不是外提交处理函数。而且,它们只发生在异步事件之后,所以默认的提交过程永远不会被阻止。

只是防止默认浏览器提交

$('#change_pass_form').submit(function (event) { 
    event.preventDefault(); 
+0

完美。谢谢 – Drewy