代码之家  ›  专栏  ›  技术社区  ›  Drewy

等待Bootbox确认运行Ajax

  •  2
  • Drewy  · 技术社区  · 7 年前

    我的代码是,

    $('#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 回复  |  直到 7 年前
        1
  •  1
  •   charlietfl    7 年前

    所有的 return false return true

    只需阻止默认浏览器提交

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