问题
当通过传入远程 URL 生成模态时,我无法让 shown.bs.modal
正确触发。
在下面的代码中,hidden.bs.modal
始终有效。
代码
$('#my_modal').on("shown.bs.modal", set_up_modal);
$('#my_modal').on("hidden.bs.modal", tear_down_modal);
$('#my_modal').modal({ remote: target_url });
set_up_modal = function() { console.log('up') };
tear_down_modal = function() { console.log('down') };
我尝试过的
我有read the docs .
我尝试将我的 .on
更改为更像 $('body').on("shown.bs.modal", '#my_modal', saa.set_up_modal) ;
但这没有产生任何变化(再次 hidden.bs.modal
起作用)。
更新
我添加了 console.log($._data( $('#my_modal')[0], "events"));
并且可以确认 显示
被绑定(bind)到对象,只是没有被调用。
我尝试使用 show.bs.modal
代替,这可行,但我需要元素在屏幕上可见,以便我对它们执行操作。
最佳答案
从这里的拉取请求中找到了一个临时解决方案 https://github.com/twbs/bootstrap/commit/4b1a6e11326fee97a5ebc194be040086f40f97fb
按照下面的方式编辑 modal.js 文件的第 81 行已经为我解决了这个问题,直到提出拉取请求
- that.$element.find('.modal-dialog') // wait for modal to slide in
+ that.$element // wait for modal to slide in
关于javascript - 如何让 shown.bs.modal 在模式使用远程 URL 时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18405512/