¿Evitar que el modo Bootstrap desaparezca al hacer clic fuera o presionar escape?


Estoy usando el modal Bootstrap de Twitter como una ventana de asistente, y me gustaría evitar que el usuario lo cierre al hacer clic fuera del modal o al presionar escape. En su lugar, quiero que se cierre cuando el usuario presione el botón finalizar. ¿Cómo podría lograr este escenario?

Author: Kasaku, 2013-04-22

13 answers

Si usa JavaScript entonces:

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

Y si HTML:

<a data-controls-modal="your_div_id" data-backdrop="static" data-keyboard="false" href="#">
 681
Author: Ehsan Zargar Ershadi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-05 12:01:07

También funciona, data-backdrop="static" para hacer clic y data-keyboard="false" para Esc en su modal div:

<div id="idModal" class="modal hide" data-backdrop="static" data-keyboard="false">
 193
Author: CesarMiguel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-07 16:07:12

Simplemente agregue data-backdrop="static" y data-keyboard="false" atributos a ese modal.

Eg.

<a data-controls-modal="your_div_id" data-backdrop="static" data-keyboard="false" href="#">
 35
Author: Ganesh Putta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-25 07:27:16

También puede Usar Direct en el modelo bootstrap.

<div class="modal fade" id="myModal" data-keyboard="false" data-backdrop="static">
 23
Author: tarun chotia,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-12 10:11:16

Puede utilizar el siguiente código

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

Para cambiar el comportamiento predeterminado.

 21
Author: Rafael Keller,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-19 21:12:05
jQuery('#modal_ajax').modal('show', {backdrop: 'static', keyboard: false});
 14
Author: Selva Balaji,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-01 06:35:09

Utilizo estos atributos y funciona, data-keyboard="false" data-backdrop="static"

 14
Author: Arsalan Ahmed Khan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-04 20:08:22
<button class="btn btn-primary btn-lg" data-backdrop="static" data-keyboard="false" data-target="#myModal" data-toggle="modal">
 9
Author: Ramesh kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-26 06:14:18

Si necesita desactivar hacer clic fuera, pero habilitar pulsando escape

$('#myModal').modal({
    backdrop: 'static',   // This disable for click outside event
    keyboard: true        // This for keyboard event
})
 5
Author: HoangLong85,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-04 04:56:07

El siguiente script funcionó para mí:

// prevent event when the modal is about to hide
$('#myModal').on('hide.bs.modal', function (e) {
    e.preventDefault();
    e.stopPropagation();
    return false;
});
 4
Author: Nabid,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-15 23:16:10

Su código funciona cuando hago clic en el lado del modal, pero si uso html input campo dentro del cuerpo modal, luego enfoca el cursor en esa entrada y luego presiona esc llave el modal ha cerrado. Haga clic en aquí

 1
Author: Marudhu Raj,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-13 06:23:47
<div class="modal show">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">     

    <h4 class="modal-title">Modal title</h4>
     </div>
     <div class="modal-body">
      <p>One fine body&hellip;</p>
  </div>

  </div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->

Creo que este codepen puede ayudarte prevent modal close css and bootstrap

 0
Author: shubhangi singh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-22 07:36:07

Si necesita configurar este después de se muestra el modal, puede usar la solución @Nabid. Sin embargo, a veces todavía necesita permitir algún método para cerrar el modal. Suponiendo que tenga un botón con la clase really-close-the-modal, que realmente debería cerrar el modal, puede usar este código (jquery):

var closeButtonClicked = false;

$('.really-close-the-modal').on('click', function () {
    closeButtonClicked = true;
});

$('#myModal').on('hide.bs.modal', function (e) {
    if (!closeButtonClicked) {
        e.preventDefault();
        e.stopPropagation();
        return false;
    }
    closeButtonClicked = false;
});

Esto no es realmente un buen diseño de código, pero me ayudó en una situación en la que el modal se mostraba con una animación del cargador, hasta que regresó una solicitud ajax, y solo entonces podía saber si el modal necesitaba ser configurado para evitar el cierre "implícito". Puede utilizar un diseño similar para evitar el cierre del modal mientras todavía está cargando.

 0
Author: youen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-06 08:36:42