Prompt a warning message before closing a modal

2019-08-05 01:19发布

问题:

I have a modal and i closing the dialog on clicking outside the modal dialog using backdrop property . I have a form in modal. If any changes are made in form and click outside the modal i should warn the user about unsaved changes. The code i am using

  $(window).on('hide.bs.modal', function() { 
       var changed_data = $("#formid").serialize();
                if(changed_data!=original_data)
                {
                    var result = confirm("Are you sure ?");
                    if(result == true){
                       $('#dialog').data('bs.modal').options.backdrop = true;
                    }
                    else
                    {
                      $('#dialog').data('bs.modal').options.backdrop = "static";
                    }
                }
                else
                {
                    $('#dialog').data('bs.modal').options.backdrop = true;
                }
            });

If i click outside the dialog the above function is calling and backdrop option value is changing but the changes are reflecting in the next call of dialog(again if i open the dialog).