如何使用甜蜜的警报来删除dropify.js的cofirm图像?

时间:2018-06-07 17:55:00

标签: 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 + "\" ?");
    }
});

4 个答案:

答案 0 :(得分:0)

好的,所以input元素通过事件处理程序传递,所以如果用户真的想删除它,你所要做的就是默认情况下在处理程序上返回false, 然后 如果用户单击swal实例上的确认,则只需输入input元素的值。我没有你的代码,但这应该有效。

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;

    }
});

答案 1 :(得分:0)

我读了答案但对我没用,但是我写了一个不同的主意

browser-ponyfill.js

答案 2 :(得分:0)

此代码还将解决单击甜蜜弹出窗口的取消按钮时的问题

var drEvent = $('.dropify').dropify();

var hideConfirmBox = false;

drEvent.on('dropify.beforeClear', function(event, element){

    if (hideConfirmBox == false) {
        swal({
            title: 'Do you want to remove?',
            text: 'This will remove the selected image.',
            buttons: {
              cancel: true,
              confirm: {
                text: 'Yes, remove it!',
                value: 'proceed'
              }
            },
            dangerMode: true
        }).then((value) => {
            if (value == 'proceed') {
                $("body").trigger("click")
                hideConfirmBox = true;
                $(this).next('button.dropify-clear').trigger('click');
            }
        });
    }


    return hideConfirmBox;
});

drEvent.on('dropify.afterClear', function(event, element){
    hideConfirmBox = false;

    // call ur ajax here for deleting on clicking yes in sweet alert pop-up

});

答案 3 :(得分:0)

也许这会起作用!

dropifyUpload.on('dropify.beforeClear', function (event, element) {

        swal({
            title: "¿Esta seguro que desea eliminarlo?",
            text: "¡Una vez confirmado no habrá vuelta atrás!",
            icon: 'warning',
            buttons: {
                cancel: 'Cancelar',
                delete: 'OK'
            }
        }).then(function (result) {
            if (result == "delete") {
                element.input.trigger($.Event("dropify.afterClear"), [element]);
            }
        })

        return false;
    });