bootstrap监听模态框_bootstrap,模态对话框,shown.bs.modal事件不会触发

在使用Bootstrap模态对话框时遇到一个问题,首次打开模态框时,'shown.bs.modal'事件不会触发,而第二次打开时才生效。经过调查发现,Bootstrap脚本中与过渡效果相关的一行代码在第一次打开时未执行。通过禁用过渡效果作为临时解决办法,事件可以在第一次打开时触发,但event.target为空字符串。第二次打开时,event.target包含正确的对话框HTML。最终,通过StackOverflow上的答案找到解决方案,即手动打开模态框并自定义Ajax加载方式。

I'm using modal dialog with remote option:

data-toggle="modal" class="banner-slide-control">Edit

Where:

Also, I'm listening for shown.bs.modal event where I use event.target property:

$("body").on("shown.bs.modal", function (event) {

// do something with event.target

}

Some reason this event is not fired when I open dialog for the first time. And it gets fired for the second time only. I tried to browse bootstrap scripts and found this code (see my comment):

var e = $.Event('shown.bs.modal', { relatedTarget: _relatedTarget })

transition ?

that.$element.find('.modal-dialog') // wait for modal to slide in

.one($.support.transition.end, function () {

that.$element.focus().trigger(e) //THIS LINE NEVER EXECUTED AT FIRST DIALOG OPENING

})

.emulateTransitionEnd(300) :

that.$element.focus().trigger(e)

So, I turned off transition as a workaround, It made event be fired for the first time, but, event.target is empty string. For the second time event.target contains appropriate dialog HTML. Is this problem with my code or bootstrap?

解决方案

I had the exact same Problem. I could fix it with the solution to this StackOverflow question: Bootstrap modal 'loaded' event on remote fragment

Basically you have to open the modal manually and implement the Ajax loading yourself. Something like:

$modal.modal({

'show': true

}).load('/Banner/SlideEditModal/1/1', function (e) {

// this is executed when the content has loaded.

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值