解雇模态后的行动bootstrap

Action after dismiss modal bootstrap

我有这个功能来创建我的模态:

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…
})

你还能给我一些建议吗?

谢谢!!

添加元素到页面后是否绑定?

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");
<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>


如果您在外部绑定事件,则需要使用事件委托

$(document).on('hidden.bs.modal', '#modalToRemove', function () {
    alert("xxx");
});