按钮调用模式对话框:单击按钮时,会触发事件,结果事件引用 e.latedTarget 未定义。那么,如何从处理程序中获取调用按钮呢? 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>
jQuery:
$('#myModal').on('show.bs.modal', function (e) {
console.log(e.relatedTarget) // do something...
})
参考:http://getbootstrap.com/javascript/#modals http://getbootstrap.com/javascript/#modals
谢谢Geo1004..我已经在使用JS来触发模态上的“show”事件,但我错过了第二个参数。所以 event.latedTarget 是未定义的。
如果其他人走 JS 路线(而不是使用数据属性),我会确保您将按钮作为 jQuery 对象发送 -
$( "#myModal" ).modal( "show", $( "#buttonBeingClicked" ) );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)