Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Members,

I am doing project in mvc. I have my forms in bootstrap model for saving and i need to show a message,if i typed anything on any input box and click on close.

"Data Not Saved. Are you Sure you want to leave the form with out saving the data" i need to show a confirmation message to users.


please help

What I have tried:

var unsaved = false;
$(":input").change(function () {
unsaved = true;
});



$('#createJobFunction').on('hidden.bs.modal', function (e) {
alert("Job Function Not Saved. Are you Sure you want to leave with out saving the data?");
})




i tried calling 'hidden.bs.modal' event of model. but his fires on submit also. i dnt want this to be fired when i submit the form
Posted
Updated 28-Jul-16 2:56am

1 solution

try this

HTML
<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

    <script>


        $(function () {

            var unsaved = false;
            $(":input").change(function () {
                unsaved = true;
            });

            $('#btnCancel').click(function () {
                if (unsaved) {
                    var flag = confirm("Job Function Not Saved. Are you Sure you want to leave with out saving the data?");
                    if (flag)
                        $('#mymodal').modal('hide');

                }
                else
                    $('#mymodal').modal('hide');

            });

        });
    </script>

</head>
<body>


    <h4>Departments</h4>
    <a class="btn btn-success" data-toggle="modal" data-target="#mymodal">
        <span class="glyphicon glyphicon-eye-open"></span>View
    </a>
    <div class="modal fade" id="mymodal">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button data-dissmiss="modal" class="close"><span>&times;</span> </button>
                    <div class="modal-title">My modal</div>
                </div>
                <div class="modal-body">

                    <input type="text" />
                    <input type="text" />
                </div>
                <div class="modal-footer">
                    <button class="btn btn-primary" id="btnSubmit">Submit</button>
                    <button class="btn btn-default" id="btnCancel">Cancel</button>

                </div>

            </div>
        </div>
    </div>


</body>
</html>



demo : Bootstrap Modal Cancel event - JSFiddle[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900