【发布时间】:2014-11-26 21:43:53
【问题描述】:
我正在尝试显示带有动态加载信息的 TwitterBootstrap 模态窗口。我想根据单击的按钮加载此信息。为此,我尝试使用 event.relatedTarget() 函数,就像文档中推荐的那样。
很遗憾,这不会返回单击的按钮,因此我无法访问所需的数据类型。
<button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal" data-type="close_service" data-id="<?php echo $currentServices["id"]; ?>">Close Service</button>
$("#myModal").on('show.bs.modal', function (event) {
console.log("modal opening"); // This properly logs, so nothing wrong with opening
var button = $(event.relatedTarget); // Button that triggered the modal
console.log(button); // returns n.fn.init[0] with a bunch of info that has nothing to do with my button
var type = button.data('type'); // Extract info from data-* attributes
console.log(type); // returns undefined
});
感谢您的帮助。
【问题讨论】:
-
看来您的代码运行良好bootply.com/Wk7DlcNEei。检查控制台。
-
我很困惑。知道为什么它不能在我的本地主机上工作吗?我会继续玩它
标签: jquery twitter-bootstrap bootstrap-modal