我想使用Ajax在模态窗口中插入内容,因此我尝试手动打开模态窗口。 代码如下:
$(function(){
$('#modal-from-dom').modal({
backdrop: true,
keyboard: true
});
$('#modalbutton').click(function(){
$('#my-modal').bind('show', function () {
// do sth with the modal
});
$('#modal-from-dom').modal('toggle');
return false;
});
});
这个 HTML 是直接从 Bootstrap JS 页面复制的。
<div id="modal-from-dom" class="modal hide fade">
<div class="modal-header">
<a href="#" class="close">×</a>
<h3>Modal Heading</h3>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<a href="#" class="btn primary">Primary</a>
<a href="#" class="btn secondary">Secondary</a>
</div>
</div>
<button id="modalbutton" class="btn">Launch Modal</button>
所以问题在于第一次单击按钮时似乎正常工作,但在第二次单击后,模态窗口会显示1秒左右然后消失。如果我将“toggle”更改为“show”,则在第二次单击后,背景不会完全淡出。如何调试此问题?
.modal()
是某种插件吗?#my-modal
是什么,你在做什么?可能的问题是你在每次点击时绑定了show
事件,并且它会干扰你的切换。 - Ilia Gdata-backdrop="false"
),如果您不需要它:<div class="modal fade" data-backdrop="false"…>
;请参阅 https://dev59.com/QWEi5IYBdhLWcg3wwegN#36087387。 - 0x6d6c