I've the same problem just today. This is my solution (which I think is better and simpler):
<!-- Modal dialog -->
<div class="modal fade" id="frmPrenotazione" tabindex="-1">
<!-- CUTTED -->
<div id="step1" class="modal-footer">
<button type="button" class="glyphicon glyphicon-erase btn btn-default" id="btnDelete"> Delete</button>
</div>
</div>
<!-- Modal confirm -->
<div class="modal" id="confirmModal" style="display: none; z-index: 1050;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body" id="confirmMessage">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" id="confirmOk">Ok</button>
<button type="button" class="btn btn-default" id="confirmCancel">Cancel</button>
</div>
</div>
</div>
</div>
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?";_x000D_
$('#btnDelete').on('click', function(e){_x000D_
confirmDialog(YOUR_MESSAGE_STRING_CONST, function(){_x000D_
//My code to delete_x000D_
console.log("deleted!");_x000D_
});_x000D_
});_x000D_
_x000D_
function confirmDialog(message, onConfirm){_x000D_
var fClose = function(){_x000D_
modal.modal("hide");_x000D_
};_x000D_
var modal = $("#confirmModal");_x000D_
modal.modal("show");_x000D_
$("#confirmMessage").empty().append(message);_x000D_
$("#confirmOk").unbind().one('click', onConfirm).one('click', fClose);_x000D_
$("#confirmCancel").unbind().one("click", fClose);_x000D_
}
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>_x000D_
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>_x000D_
_x000D_
<!-- Modal dialog -->_x000D_
<div id="frmTest" tabindex="-1">_x000D_
<!-- CUTTED -->_x000D_
<div id="step1" class="modal-footer">_x000D_
<button type="button" class="glyphicon glyphicon-erase btn btn-default" id="btnDelete"> Delete</button>_x000D_
</div>_x000D_
</div>_x000D_
_x000D_
<!-- Modal confirm -->_x000D_
<div class="modal" id="confirmModal" style="display: none; z-index: 1050;">_x000D_
<div class="modal-dialog">_x000D_
<div class="modal-content">_x000D_
<div class="modal-body" id="confirmMessage">_x000D_
</div>_x000D_
<div class="modal-footer">_x000D_
<button type="button" class="btn btn-default" id="confirmOk">Ok</button>_x000D_
<button type="button" class="btn btn-default" id="confirmCancel">Cancel</button>_x000D_
</div>_x000D_
</div>_x000D_
</div>_x000D_
</div>
_x000D_