我有这个功能来创建我的模态:
function modalAlert(text)
{
if($('#modalToRemove'))
$('#modalToRemove').remove();
$('<div id="modalToRemove" class="modal fade"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button><h4 class="modal-title">TITLE</h4></div><div class="modal-body"><h4>'+text+'</h4></div><div class="modal-footer"> <a class="btn btn-default" data-dismiss="modal">Close</a></div></div></div></div>').modal();
}
我想在关闭模态后做一个动作。 我试过这段代码但没有工作......
$('#myModal').on('hidden.bs.modal', function () {
// do something…
})
你能告诉我其他什么吗?
谢谢!
答案 0 :(得分:0)
将元素添加到页面后,是否绑定了它?
function modalAlert (text) {
$('#modalToRemove').remove();
$('<div id="modalToRemove" class="modal fade"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button><h4 class="modal-title">TITLE</h4></div><div class="modal-body"><h4>'+text+'</h4></div><div class="modal-footer"> <a class="btn btn-default" data-dismiss="modal">Close</a></div></div></div></div>').modal();
$('#modalToRemove').on('hidden.bs.modal', function () {
alert("xxx");
});
}
modalAlert("hey");
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
&#13;
如果您要在其外部绑定事件,则需要使用事件委派
$(document).on('hidden.bs.modal', '#modalToRemove', function () {
alert("xxx");
});