Bootstrap 3 Modal:如何使用 jquery/javascript 检查模态是打开还是关闭
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24949383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bootstrap 3 Modal: how to check if modal is open or closed using jquery/javascript
提问by F Shahzad
Can anyone please tell me how to check bootstrap 3.0 modal status, is it open or closed using jQuery or javascript. I used following code but it works when you have opened a modal for one time otherwise gives data undefined error.
谁能告诉我如何检查 bootstrap 3.0 模式状态,它是使用 jQuery 还是 javascript 打开还是关闭。我使用了以下代码,但是当您打开模态一次时它可以工作,否则会出现数据未定义错误。
if($('#addMemberModal').data('bs.modal').isShown == true){
console.log("Modal is open");
}
回答by alexoviedo999
You can also use straight jQuery like this:
你也可以像这样直接使用 jQuery:
$('#myModal').is(':visible');
回答by Se0ng11
you can refer to their page http://getbootstrap.com/javascript/#modals
你可以参考他们的页面 http://getbootstrap.com/javascript/#modals
$('#myModal').on('hidden.bs.modal', function (e) {
// do something...
})
show.bs.modal
This event fires immediately when the show instance method is called. If caused by a click, the clicked element is available as the relatedTarget property of the event.
show.bs.modal
该事件在调用 show 实例方法时立即触发。如果由单击引起,则被单击的元素可用作事件的 relatedTarget 属性。
shown.bs.modal
This event is fired when the modal has been made visible to the user (will wait for CSS transitions to complete). If caused by a click, the clicked element is available as the relatedTarget property of the event.
show.bs.modal
当模态对用户可见时触发此事件(将等待 CSS 转换完成)。如果由单击引起,则被单击的元素可用作事件的 relatedTarget 属性。
hide.bs.modal
This event is fired immediately when the hide instance method has been called.
hide.bs.modal
当调用 hide 实例方法时,会立即触发此事件。
hidden.bs.modalThis event is fired when the modal has finished being hidden from the user (will wait for CSS transitions to complete). loaded.bs.modal This event is fired when the modal has loaded content using the remote option.
hidden.bs.modal当模态完成对用户隐藏时触发此事件(将等待 CSS 转换完成)。Loaded.bs.modal 当模态使用远程选项加载内容时会触发此事件。
回答by Sudhir Bastakoti
try checking:
尝试检查:
if($("#addMemberModal").data('modal') && $("#addMemberModal").data('modal').isShown ) {
console.log("Modal is open");
}
or
或者
if( $('#addMemberModal').hasClass('in') ) {
console.log("Modal is open");
}
回答by CelzioBR
if the modal is opened in a form loaded by ajax you can use
如果模式是在 ajax 加载的表单中打开的,您可以使用
$(document).on('hidden.bs.modal', '#photoModal', function () {
cancel_camera();
document.getElementById('PhotoPerson').innerHTML = '<img src="' + photoPerson + '"/>';
});*
回答by Taranis
I used the following code for my AJAX Call. After a setTimeout i tried to close the model properly and used the is() method for doing so. Hope it helps somebody.
我将以下代码用于我的 AJAX 调用。在 setTimeout 之后,我尝试正确关闭模型并使用 is() 方法这样做。希望它可以帮助某人。
complete: function(){
setTimeout(function() {
// model
if($('#mdlNewOrder').is(':visible')) {
$('#mdlNewOrder').modal('toggle');
}
//buttons
$('#btn_closeNewOrder').prop("disabled", false);
$('#btn_NewOrder').prop("disabled", false);
// table reload
$('#table_patientlist').DataTable().ajax.reload(null, false);
}, 2800);
}