我试图确保每次显示模态对话框时,都没有可见对话框。为此,我正在做:
$('.modal').modal('hide');
但是这样做,当我显示一个模态对话框时:
var dlg_opts = { show: true, keyboard: false, backdrop: 'static' };
如果我点击背景 (backdrop) 或按 escape,模式将关闭。 当我删除 .modal('hide') 时,代码运行良好,模式仅通过“关闭”按钮关闭。
有一个我的模态窗口的例子:
<div class="modal fade" id="dlgWarning" tabindex="-1" role="dialog" aria-labelledby="dlgWarningLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="dlgWarningLabel"></h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
<button type="button" class="btn btn-warning" id="btnWarningAccept"></button>
</div>
</div>
</div>
</div>
这是对话框的代码:
var dlg_opts = { show: true, keyboard: false, backdrop: 'static' };
function ShowModal(id, width) {
CloseModal();
id = '#' + id;
$(id).modal(dlg_opts);
if (arguments.length == 2) $(id).css('width', width + 'px');
}
function CloseModal() {
$('.modal').modal('hide');
}
有人知道它会是什么吗? 非常感谢!
编辑:
在这里你可以测试它:http://www.bootply.com/123369 如果您尝试这样做,您将看到对话框以 esc/click 关闭。评论 $('.modal').modal('hide') 代码效果很好!
最佳答案
这是因为每次你想调用它时你都重新初始化模态......
所以首先初始化模态而不显示它们。
Bootstrap :http://www.bootply.com/123372
JS :
var dlg_opts = { show: false, keyboard: false, backdrop: 'static' }; // <-- HERE
$('.modal').modal(dlg_opts); // <-- HERE
function ShowModal(id, width) {
CloseModal();
id = '#' + id;
$(id).modal('show'); // <-- HERE
if (arguments.length == 2) $(id).css('width', width + 'px');
}
function CloseModal() {
$('.modal').modal('hide');
}
$(document).ready(function(){
ShowModal('dlgWarning');
});
HTML : 与给定的相同
关于javascript - 手动隐藏后的 Bootstrap 模态错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22533317/