[英]Action after dismiss modal bootstrap
I have this function to create my modal: 我有此功能来创建我的模态:
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();
}
And I would like to do an action after closing the modal. 我想在关闭模态后做一个动作。 I tried this code but not working...
我尝试了此代码,但无法正常工作...
$('#myModal').on('hidden.bs.modal', function () {
// do something…
})
Can you suggest me any thing else? 你能建议我其他吗?
Thanks!! 谢谢!!
Are you binding it after you add the element to the page? 将元素添加到页面后是否要绑定它?
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>
If you are binding the event outside of it, you need to use event delegation 如果要绑定事件之外的事件,则需要使用事件委托
$(document).on('hidden.bs.modal', '#modalToRemove', function () {
alert("xxx");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.