Confirm deletion using Bootstrap 3 modal box

后端 未结 8 610
走了就别回头了
走了就别回头了 2020-11-30 19:57

I need to confirm deletion using Bootstrap 3 modal box (YES/NO). How can I create this?

HTML code:

&
8条回答
  •  伪装坚强ぢ
    2020-11-30 20:09

    Create modal dialog in HTML with id="confirmation" and use function showConfirmation.

    Also remember you should to unbind (modal.unbind()) cancel and success buttons after hide modal dialog. If you do not make this you will get double binding. For example: if you open dialog once and press 'Cancel' and then open dialog in second time and press 'Ok' you will get 2 executions of success callback.

    showConfirmation = function(title, message, success, cancel) {
        title = title ? title : 'Are you sure?';
        var modal = $("#confirmation");
        modal.find(".modal-title").html(title).end()
            .find(".modal-body").html(message).end()
            .modal({ backdrop: 'static', keyboard: false })
            .on('hidden.bs.modal', function () {
                modal.unbind();
            });
        if (success) {
            modal.one('click', '.modal-footer .btn-primary', success);
        }
        if (cancel) {
            modal.one('click', '.modal-header .close, .modal-footer .btn-default', cancel);
        }
    };
    
    // bind confirmation dialog on delete buttons
    $(document).on("click", ".delete-event, .delete-all-event", function(event){
        event.preventDefault();
        var self = $(this);
        var url = $(this).data('url');
        var success = function(){
            alert('window.location.href=url');
        }
        var cancel = function(){
            alert('Cancel');
        };
        if (self.data('confirmation')) {
            var title = self.data('confirmation-title') ? self.data('confirmation-title') : undefined;
            var message = self.data('confirmation');
            showConfirmation(title, message, success, cancel);
        } else {
            success();
        }
    });
    

    https://jsfiddle.net/yiiBoy/hne9sp6g/

提交回复
热议问题