防止引导-3模式在表单发生变化时关闭 [英] Prevent bootstrap-3 modal from closing when the form has changes
本文介绍了防止引导-3模式在表单发生变化时关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图阻止bootstrap-3模式在没有警告的情况下关闭模态内的窗体。但是,当我听到模态触发的事件并返回false时,它将阻止模态永远关闭。这里是我的代码:
I'm trying to prevent the bootstrap-3 modal from closing without warning when there are changes made to the form inside the modal. However when I listen to the events fired by the modal and return false it will prevent the modal from closing ever. Here's my 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 you sure?')) {
return false;
}
}
});
});
所以简而言之,在这种情况下,如何防止模式关闭一次。
So in short, in this case; how do I prevent the modal from closing just this once.
推荐答案
好的,我想通了,而不是返回false我需要to event.preventDefault()
Ok so I figured it out, instead of return false I needed to event.preventDefault()
jsfiddle here: http: // $ jsfiddle.net/ACYBv/1/
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 the form data was changed since the modal was openened
if($(this).data('form-data') != $(this).find('form').serialize()) {
if(!confirm('you sure??')) {
e.preventDefault();
}
}
});
});
这篇关于防止引导-3模式在表单发生变化时关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文