javascript - Prevent bootstrap-3 modal from closing when the form has changes -
i'm trying prevent bootstrap-3 modal closing without warning when there changes made form inside modal. when listen events fired modal , return false prevent modal closing ever. here's code:
$(function() { $('body').live('shown.bs.modal', '#quickbutton-create', function () { $(this).find('#quickbutton-create form').monitor(); }); $('body').live('hide.bs.modal', '#quickbutton-create', function () { if ($(this).find('#quickbutton-create form').monitor('has_changed')) { if (!confirm('are sure?')) { return false; } } }); });
so in short, in case; how prevent modal closing once.
ok figured out, instead of return false needed event.preventdefault()
jsfiddle here: http://jsfiddle.net/acybv/1/
$(function() { $('.modal').on('shown.bs.modal, loaded.bs.modal', function(e) { // set form state $(this).data('form-data', $(this).find('form').serialize()); }); $('.modal').on('hide.bs.modal', function(e) { // check if form data changed since modal openened if($(this).data('form-data') != $(this).find('form').serialize()) { if(!confirm('you sure??')) { e.preventdefault(); } } }); });
Comments
Post a Comment