Home > Backend Development > PHP Tutorial > javascript - How to avoid form submission also triggering beforeunload event

javascript - How to avoid form submission also triggering beforeunload event

WBOY
Release: 2016-08-04 09:21:31
Original
1436 people have browsed it

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>
Copy after login
Copy after login

Just unbind it in the submit event of the button

<code>$(window).off('beforeunload')或者window.onbeforeunload = null;
</code>
Copy after login
Copy after login

Reply content:

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>
Copy after login
Copy after login

Just unbind it in the submit event of the button

<code>$(window).off('beforeunload')或者window.onbeforeunload = null;
</code>
Copy after login
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template