简体   繁体   English

bootstrap,模态对话框,showed.bs.modal 事件不会触发

[英]bootstrap, modal dialogs, shown.bs.modal event doesn't fire

I'm using modal dialog with remote option:我正在使用带有远程选项的模式对话框:

<a target="profile-banner" data-target="#edit-slide-dlg" href="/Banner/SlideEditModal/1/1"
data-toggle="modal" class="banner-slide-control">Edit</a>

Where:哪里:

<div id="edit-slide-dlg" class="modal fade" tabindex="-1"></div>

Also, I'm listening for shown.bs.modal event where I use event.target property:另外,我正在监听我使用 event.target 属性的 show.bs.modal 事件:

$("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.所以,我关闭了转换作为一种解决方法,它第一次触发了事件,但是 event.target 是空字符串。 For the second time event.target contains appropriate dialog HTML.第二次 event.target 包含适当的对话框 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我可以使用此 StackOverflow 问题的解决方案来修复它: 远程片段上的 Bootstrap modal 'loaded' 事件

Basically you have to open the modal manually and implement the Ajax loading yourself.基本上你必须手动打开模态并自己实现 Ajax 加载。 Something like:类似的东西:

$modal.modal({
    'show': true
}).load('/Banner/SlideEditModal/1/1', function (e) {
    // this is executed when the content has loaded.
});

For anyone coming here late and wading through lots of related issues, this answer from related post solved the OPs issue exactly for me...对于迟到并处理许多相关问题的人来说, 相关帖子的这个答案正好为我解决了 OP 问题......

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM