简体   繁体   English

使用窗体的window.onbeforeunload

[英]window.onbeforeunload with a form

I will try to explain this correctly: 我将尝试正确解释这一点:

I have a website with a huge form on it. 我有一个网站,上面有一个巨大的表格。 This form has a ton of fields and it all run by another JS file. 此表单具有大量字段,并且都由另一个JS文件运行。

To be exact: in my form I have text box's and drop down menus. 确切地说:在我的表单中,我具有文本框的和下拉菜单。 I want to be able to "reset/refresh" my page after someone leaves it. 我希望有人离开后可以“重置/刷新”我的页面。 There's a button at the bottom of the foam that takes you away from the site into a shopping cart. 泡沫底部有一个按钮,可让您从站点移至购物车。

After going to the shopping cart I want to be able to click the back button and have all the fields reset back to the original state. 转到购物车后,我希望能够单击“后退”按钮,并将所有字段重置为原始状态。 I tried putting this near the top but it's having no effect. 我尝试将其放在顶部,但没有任何效果。 Maybe i'm sticking it in the wrong place?: 也许我将其粘贴在错误的位置?:

<script type="text/javascript">
window.onbeforeunload = function () {
    }
   </script>

You can try resetting the form in the onload event, which will happen on first load of the form, and also when the user navigates back to the form with the back button: 您可以尝试在onload事件中重置表单,该事件将在第一次加载表单时以及在用户使用“后退”按钮导航回到表单时发生:

window.onload = function() {
    var frm = document.form1;   // put form name here
    for (var i = 0; i < frm.elements.length; i++)
        frm.elements[i].value = "";
};

Demo: http://jsfiddle.net/bavRd/ 演示: http//jsfiddle.net/bavRd/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM