2017-08-06 91 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