您不是唯一缺少该功能的人。我觉得bootstrap有时候太“简约”,后面的人有很多想法应该在“实现层”做,但是当bootstrap jQuery插件本身无法实现时,它就没有用了!
你必须自己实现这个功能,像这样:
在bootstrap.js v2.1.1 modal 中从第61行开始。
在Modal.prototype中,添加两个函数,lock和unlock,看起来是这样的(我这里只显示modal.prototype的开头,因为代码太多了)
Modal.prototype = {
constructor: Modal
//add this function
, lock: function () {
this.options.locked = true
}
//add this function
, unlock: function () {
this.options.locked = false
}
, toggle: function () {
...
...
然后,同样在 Modal.prototype 中,找到函数 hide,并添加一行,使其看起来像这样(再次,只显示隐藏的顶部)
, hide: function (e) {
e && e.preventDefault()
var that = this
//add this line
if (that.options.locked) return
e = $.Event('hide')
...
...
最后,将$.fn.modal.defaults 更改为:
$.fn.modal.defaults = {
backdrop: true
, keyboard: true
, show: true
, locked: false //this line is added
}
现在您的引导模式中具有即时锁定/解锁功能,可防止用户在关键时刻关闭模式。
示例:
这是来自http://twitter.github.com/bootstrap/javascript.html#modals的“现场演示”的修改版本
<!-- Button to trigger modal -->
<a href="#myModal" role="button" class="btn" data-toggle="modal">Launch demo modal</a>
<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Modal header</h3>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn btn-primary" onclick="$('#myModal').modal('lock');">lock</button>
<button class="btn btn-primary" onclick="$('#myModal').modal('unlock');">unLock</button>
</div>
</div>
<script type="text/javascript">
我插入了两个按钮,“锁定”和“解锁” - 单击时,它们将模式设置为锁定或正常模式(初始化设置)
编辑,在你的情况下,你只需要在做 ajax 时调用 lock/onlock :
$("myModal").modal('lock');
$.ajax({
url: url,
...
...
, success(html) {
...
...
$("#myModal").modal('unlock');
}
});