Confirm deletion using Bootstrap 3 modal box

后端 未结 8 611
走了就别回头了
走了就别回头了 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:01

    I've the same problem just today. This is my solution (which I think is better and simpler):

    
    
    
    
    
    

    And in my .js:

    $('#btnDelete').on('click', function(e){
        confirmDialog(YOUR_MESSAGE_STRING_CONST, function(){
            //My code to delete
        });
    });
    
    function confirmDialog(message, onConfirm){
        var fClose = function(){
            modal.modal("hide");
        };
        var modal = $("#confirmModal");
        modal.modal("show");
        $("#confirmMessage").empty().append(message);
        $("#confirmOk").unbind().one('click', onConfirm).one('click', fClose);
        $("#confirmCancel").unbind().one("click", fClose);
    }
    

    Using unbind before the one prevents that the removal function is invoked at the next opening of the dialog.

    I hope this could be helpful.

    Follow a complete example:

    var YOUR_MESSAGE_STRING_CONST = "Your confirm message?";
          $('#btnDelete').on('click', function(e){
        		confirmDialog(YOUR_MESSAGE_STRING_CONST, function(){
        			//My code to delete
              console.log("deleted!");
        		});
        	});
    
            function confirmDialog(message, onConfirm){
        	    var fClose = function(){
        			  modal.modal("hide");
        	    };
        	    var modal = $("#confirmModal");
        	    modal.modal("show");
        	    $("#confirmMessage").empty().append(message);
        	    $("#confirmOk").unbind().one('click', onConfirm).one('click', fClose);
        	    $("#confirmCancel").unbind().one("click", fClose);
            }
    
    
    
    
    
    

提交回复
热议问题