Html 防止 Bootstrap Modal 在单击外部或按 Esc 时消失?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16152073/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:46:01  来源:igfitidea点击:

Prevent Bootstrap Modal from disappearing when clicking outside or pressing escape?

htmltwitter-bootstrapmodal-dialog

提问by Ehsan Zargar Ershadi

I'm using the Twitter Bootstrap modal as a wizard window, and would like to prevent the user from closing it when clicking outside of the modal or when pressing escape. Instead, I want it to be closed when the user presses the finish button. How could I achieve this scenario?

我使用 Twitter Bootstrap 模态作为向导窗口,并希望防止用户在单击模态外部或按 Esc 时关闭它。相反,我希望它在用户按下完成按钮时关闭。我怎么能实现这个场景?

回答by Ehsan Zargar Ershadi

If using JavaScript then:

如果使用 JavaScript,则:

$('#myModal').modal({
    backdrop: 'static',
    keyboard: false
})

or in HTML:

或在 HTML 中:

<a data-controls-modal="your_div_id" data-backdrop="static" data-keyboard="false" href="#">

回答by CesarMiguel

Works too, data-backdrop="static"to click out and data-keyboard="false"to Esc in your div modal:

data-backdrop="static"可以data-keyboard="false"在您的 div 模式中单击并退出:

<div id="idModal" class="modal hide" data-backdrop="static" data-keyboard="false">

回答by Tarun..

You can Use Direct in bootstrap model also.

您也可以在引导模型中使用 Direct。

<div class="modal fade" id="myModal" data-keyboard="false" data-backdrop="static">

回答by Ganesh Putta

Just add data-backdrop="static"and data-keyboard="false"attributes to that modal.

只需添加data-backdrop="static"data-keyboard="false"属性到该模式。

Eg.

例如。

<a data-controls-modal="your_div_id" data-backdrop="static" data-keyboard="false" href="#">

回答by Rafael Keller

You can use the code below

您可以使用下面的代码

$.fn.modal.prototype.constructor.Constructor.DEFAULTS.backdrop = 'static';

to change the default behavior.

更改默认行为。

回答by Selva Balaji

jQuery('#modal_ajax').modal('show', {backdrop: 'static', keyboard: false});

回答by Arsalan Ahmed Khan

I use these attributes and it works, data-keyboard="false" data-backdrop="static"

我使用这些属性并且它有效, data-keyboard="false" data-backdrop="static"

回答by Sumit Kumar Gupta

<button class='btn btn-danger fa fa-trash' data-toggle='modal' data-target='#deleteModal' data-backdrop='static' data-keyboard='false'></button>

simply add data-backdrop and data-keyboard attribute to your button on which model is open.

只需将 data-backdrop 和 data-keyboard 属性添加到打开模型的按钮上。

回答by Rohit Parte

This code will prevent the modal from closing if you click outside the modal.

如果您在模态外单击,此代码将阻止模态关闭。

   $(document).ready(function () {
    $('#myModal').modal({
           backdrop: 'static',
           keyboard: false
    })
   });

回答by Ramesh kumar

<button class="btn btn-primary btn-lg" data-backdrop="static" data-keyboard="false" data-target="#myModal" data-toggle="modal">