How to prevent “Are you sure you want to navigate away from this page?” alert on form submission?

前端 未结 4 649
清酒与你
清酒与你 2021-01-06 11:03

I have a simple html form with few fields in it.

I wanted to make sure that user gets an alert message when he is trying to leave the page. So I added the script on

4条回答
  •  无人及你
    2021-01-06 11:32

    Try this: http://cfsilence.com/blog/client/index.cfm/2009/10/12/jQuery-Method-To-Prompt-A-User-To-Save-Changes-Before-Leaving-Page

    I added another bit to the function:

     $('#submitButton').click(function () {
            isDirty = false;
    });
    

    Just so the form doesn't ask for validation when you want to submit it :)

    I hope this helps

    Note: You could also do something like $("input[type=submit]") if you don't like to depend on a common naming (else your submit button needs to be called submitButton)

提交回复
热议问题