javascript - 在 bootstrap-fileinput 中继续 filepredelete 事件之前等待 Sweetalert2 异步确认

标签 javascript sweetalert2 kartik-v

插件 Bootstrap 文件输入和 SweetAlert2 存在一些问题

这是为了显示一个对话框以确认用户是否真的要继续删除文件。

如果我要中止删除文件,我需要为 filepredelete 事件返回一个 bool 值。该函数是同步的,但我使用的是带有 promise 的异步插件 SweetAlert2。

这是我的代码:

$("#gallery-images").fileinput({
           ...

}).on("filepredelete", function(event, key, jqXHR, data) {

    swal({
        title: 'Are you sure you want to delete this image?',
        showCancelButton: true,
        confirmButtonText: 'Confirm',
        cancelButtonText: 'Cancel',
        type: 'warning',
    }).then((result) => {
        if(result.value){
            return false;
        }
        return true;
    });

});

我找不到等待确认的方法。我在想总是中止删除并在用户确认时手动删除文件。我需要重构我的代码。

最佳答案

尝试 promise ,我已经在同一个问题上苦苦挣扎了 3 天并弄明白了,希望这对某些人有所帮助。

$("#gallery-images").fileinput({
       ...

    }).on("filepredelete", function(event, key, jqXHR, data) {

    return new Promise(function(resolve, reject) {
            swal.fire({
                title: 'Are you sure?',
                text: 'You will not be able to recover this file!',
                icon: 'warning',
                showCancelButton: true,
                confirmButtonText: 'Yes, delete it!',
                cancelButtonText: 'No, keep it'
            }).then((result) => {
                if (result.value) {
                    resolve();
                }
            });
        });

    });

尽情享受吧!

关于javascript - 在 bootstrap-fileinput 中继续 filepredelete 事件之前等待 Sweetalert2 异步确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50442929/

相关文章:

javascript - 我可以在处理事件时依赖 attach order 吗?

javascript - sweetalert2 : how to get autofocus on input?

php - 如何在 Yii 中渲染空网格表?

javascript - sweetalert2:如何在重定向时拥有加载器动画

javascript - SweetAlert2 确认对话框在 onclick 中无法正常工作

php - Yii2 需要 "select2"小部件上的验证规则

yii - Kartik Grid 可编辑列,扩展行样式不适用

javascript - 我应该在域之后还是在应用程序 View 之后构建 Redux 商店?

javascript - 在实例化期间使用构造函数变量

javascript - 是否存在反向查询选择器?