javascript - 如何使用 Sweet Alert 来确认 dropify.js 的图像删除?

标签 javascript sweetalert dropify

我想在删除图像之前使用甜蜜的警报确认。但是,这不起作用。如果我使用 javascript 默认确认,它就可以工作。我尝试使用异步和等待,但没有帮助。我该如何修复/调试这个问题?

var pathURL = "file_path/";
var dropifyElements = {};
$('.dropify').each(function() {
    dropifyElements[this.id] = true;
});
var drEvent = $('.dropify').dropify();

drEvent.on('dropify.beforeClear', function(event, element) {
    id = event.target.id;
    if(dropifyElements[id]) {
        var x = false;
        return swal({   
            title: "Are you sure?",   
            text: "You will not be able undo this operation!",   
            type: "warning",   
            showCancelButton: true,   
            confirmButtonColor: "#DD6B55",   
            confirmButtonText: "Yes, delete it!",   
            cancelButtonText: "No, cancel please!",   
            closeOnConfirm: false,   
            closeOnCancel: false 
        }, function(isConfirm){   
            if (isConfirm) {
                x = true;
            } else {     
                swal({
                    title:"Cancelled",
                    text:"Delete Cancelled :)",
                    type:"error",
                    timer: 2000,
                });
                x = true;
            } 
        });
        return x;
        //return confirm("Do you really want to delete this image?");
        // return confirm("Do you really want to delete \"" + element.file.name + "\" ?");
    }
});

最佳答案

好的,input 元素通过事件处理程序传递,因此如果用户确实想要删除它,您所要做的就是默认在处理程序上返回 false,那么如果用户点击 swal 实例上的确认按钮,您只需取消输入元素的值即可。我没有你的代码,但这应该可以。

var pathURL = "file_path/";
var dropifyElements = {};
$('.dropify').each(function() {
    dropifyElements[this.id] = true;
});
var drEvent = $('.dropify').dropify();

drEvent.on('dropify.beforeClear', function(event, element) {
    id = event.target.id;
    if(dropifyElements[id]) {

        swal({   
            title: "Are you sure?",   
            text: "You will not be able undo this operation!",   
            type: "warning",   
            showCancelButton: true,   
            confirmButtonColor: "#DD6B55",   
            confirmButtonText: "Yes, delete it!",   
            cancelButtonText: "No, cancel please!",   
            closeOnConfirm: false,   
            closeOnCancel: false 
        }, isConfirm => {   
            if (isConfirm) {
                element.value = "";
            } else {     
                swal({
                    title:"Cancelled",
                    text:"Delete Cancelled :)",
                    type:"error",
                    timer: 2000,
                });
            } 
        });

        return false;

    }
});

关于javascript - 如何使用 Sweet Alert 来确认 dropify.js 的图像删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50747418/

相关文章:

javascript - 滚动时只播放 CSS 动画

javascript - 如何在 SweetAlert 弹出窗口中添加关闭按钮

javascript - 使用 2 个按钮 + sweetalert2 提交表单

javascript - 使用 ajax 的甜蜜警报 - 无法弄清楚发布后的返回消息

javascript - 如何更改默认文件 dropify?

javascript - 如何动态添加工作 dropify 输入

javascript - 与页面上的元素交互时防止滑动事件

javascript - Ember SyntaxError 意外的标记; NodeJS 模块相关

javascript - 重新启用文本选择(Opera、webkit、ie)