如何在提交表单时禁用“window.onbeforeunload”?

时间:2014-11-10 07:37:18

标签: javascript php jquery html forms

当我从浏览器关闭此页面时,会打开一个警告框,询问“离开此页面”或“保持此状态”即可。但是当从下面给出的提交按钮提交表单时再次询问并显示此警告框。如何在提交表单时禁用此功能,不应该询问并显示警告框?

<script>
        window.onbeforeunload = function(e) {
          return 'You application form is not submitted yet.';
        };
        WinPrint.document.write('<span style="font-size:22px">' + prtContent.innerHTML + '<span>');
    </script>

    <div>
          <input type="submit" name="" class="button" value="Save Your Application Form" onclick="if(document.getElementById('thumb').value=='') { alert('Please select your photograph.'); return false; } return confirm('Read your application form thoroughly before submitting, Once submitted data, it will not changed in any case, press OK to submit or press CANCEL to make corrections.'); return false;" style="text-align:center !important; float:none !important;"/>
    </div>

2 个答案:

答案 0 :(得分:7)

$(document).on("submit", "form", function(event){
        window.onbeforeunload = null;
});

应该做的伎俩

答案 1 :(得分:0)

您可以使用onsubmit事件并删除其中的onbeforeupload侦听器,因此,修改后的脚本:

<script>
    window.onbeforeunload = function(e) {
      return 'You application form is not submitted yet.';
    };
    document.getElementById("your_form_id_here").onsubmit = function(e) {
      window.onbeforeunload = null;
      return true;
    };
    WinPrint.document.write('<span style="font-size:22px">' + prtContent.innerHTML + '<span>');
</script>