按钮调用模态对话框:当单击按钮时,事件触发生成的事件引用e.relatedTarget未定义.那么,如何从处理程序中获取调用按钮? e似乎没有包含对调用按钮的引用.
<!-- Button trigger modal --> <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> Launch demo modal </button> <!-- Modal --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title" id="myModalLabel">Modal title</h4> </div> <div class="modal-body"> ... </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div>@H_403_3@jQuery的:
$('#myModal').on('show.bs.modal',function (e) { console.log(e.relatedTarget) // do something... })@H_403_3@
解决方法
看看下面的
Bootply example.
当运行show事件似乎包括对e.relatedTarget的适当引用.
$('#myModal').on('show.bs.modal',function (e) { var button = e.relatedTarget; if (button != null) { alert("Launch Button ID='" + button.id + "'"); } })@H_403_3@看看Bootply example,看看你自己的代码是否偏离了它. (我从您提供的link直接复制了原始的Bootstrap示例代码片段.)
我希望这有帮助.祝你好运.