【问题标题】:How to disable "window.onbeforeunload" when submit form?提交表单时如何禁用“window.onbeforeunload”?
【发布时间】:2025-11-28 06:20:06
【问题描述】:

当我从浏览器关闭此页面时,会打开一个警告框,询问“离开此页面”或“留在此页面上”这没关系。但是,当从下方给出的提交按钮提交表单时,再次询问并显示此警告框。我如何在提交表单时禁用它不应该询问并显示警告框??

<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>

【问题讨论】:

    标签: javascript php jquery html forms


    【解决方案1】:
    $(document).on("submit", "form", function(event){
            window.onbeforeunload = null;
    });
    

    应该做的伎俩

    【讨论】:

      【解决方案2】:

      您可以使用 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>
      

      【讨论】:

      • 当我提交表单时,确认()被调用,然后警报框打开,但是当我按确定时,它再次显示警报框并要求离开此页面等