Kelmen
Kelmen

Reputation: 1063

javascript bootstrap modal - cancel closing

my work is using javascript bootstrap 2.0.4

I'm looking for a way to cancel the modal from closing, by logic control. but I can't find any details how to do this

http://getbootstrap.com/javascript/#modals

something like .NET

OnWindowClosing(eventArguement) 
{ 
    if (shouldCancel) {
      eventArguement.Cancel = true; 
      return;
    }
}

Upvotes: 31

Views: 36041

Answers (3)

BENARD Patrick
BENARD Patrick

Reputation: 30975

Look at here : www.bootply.com/QTTDf3zRgV

In this exemple, if the checkbox is checked, then the modal can't be closed ( with js/jquery):

$('#myModal').on('hide.bs.modal', function(e){
  if( $('#block').is(':checked') ) {
     e.preventDefault();
     e.stopImmediatePropagation();
     return false; 
   }
});

Upvotes: 58

Affilnost
Affilnost

Reputation: 329

e.cancel is not defined and causes an error - that's why the solution above is working.

You should add e.stopImmediatePropagation();

Here's a demo: http://www.bootply.com/o5jsyItQMm

Upvotes: 5

Himanshu Singh
Himanshu Singh

Reputation: 136

the solution you provided was not working so find this and its the perfect solution referrence here https://github.com/twbs/bootstrap/issues/1202#issuecomment-48601320

$('#myModal').on('hide.bs.modal.prevent', closeModalEvent);
/**
* allow popup to hide or not
* @param {type} e
* @returns {Boolean}
 */
function closeModalEvent(e) {
    e.preventDefault();
    if ($('#block').is(':checked')) {
        $('#myModal').off('hide.bs.modal.prevent');
        $("#myModal").modal('hide');
        return true;
    }
    return false;
}

Upvotes: 4

Related Questions