Home >Backend Development >PHP Tutorial >javascript - How to avoid form submission also triggering beforeunload event
The specific requirements are as follows:
Bind the beforeunload event to the window, check whether the input content has been saved when switching or closing the page, but the beforeunload event will also be triggered when the submit button is clicked. How to prevent form submission from triggering this event?
Part of the code:
<code> if ($scope.model.isSaving) { window.onbeforeunload = function (e) { e = e || window.event; // 兼容IE8和Firefox 4之前的版本 if (e) { e.returnValue = '关闭提示'; } // Chrome, Safari, Firefox 4+, Opera 12+ , IE 9+ return '关闭提示'; }; }</code>
Just unbind it in the submit event of the button
<code>$(window).off('beforeunload')或者window.onbeforeunload = null; </code>
The specific requirements are as follows:
Bind the beforeunload event to the window, check whether the input content has been saved when switching or closing the page, but the beforeunload event will also be triggered when the submit button is clicked. How to prevent form submission from triggering this event?
Part of the code:
<code> if ($scope.model.isSaving) { window.onbeforeunload = function (e) { e = e || window.event; // 兼容IE8和Firefox 4之前的版本 if (e) { e.returnValue = '关闭提示'; } // Chrome, Safari, Firefox 4+, Opera 12+ , IE 9+ return '关闭提示'; }; }</code>
Just unbind it in the submit event of the button
<code>$(window).off('beforeunload')或者window.onbeforeunload = null; </code>
Bind button events, ajax submit form
After submitting the click event, stay on the current page and do not trigger this event! I don’t know if this works?
You can use ajax to submit the form and get the returned results.