问题
How do I know which button triggered the opening of a Bootstrap Modal Dialog?
<a href="#" data-toggle="modal" data-target="#myModal">
Button 1
</a>
<a href="#" data-toggle="modal" data-target="#myModal">
Button 2
</a>
Javascript
$('#myModal').on('shown.bs.modal', function () {
var triggerElement = ???
})
回答1:
Its documented here
$('#myModal').on('shown.bs.modal', function (event) {
var triggerElement = $(event.relatedTarget); // Button that triggered the modal
});
Edit: As noted in comments, if buttons are dynamically generated we can attach an event listener to document:
$(document).on('shown.bs.modal', '#myModal', function (event) {
var triggerElement = $(event.relatedTarget); // Button that triggered
});
来源:https://stackoverflow.com/questions/39647983/get-the-element-whose-click-opened-a-bootstrap-modal-dialog