简体   繁体   English

在后退按钮上重置表单

[英]reset the form on back button

I have searched on the web but couldn't find any solution.我在网上搜索过,但找不到任何解决方案。 When I click on the 'Submit' button on 'samplepage.php', I redirect the page to the 'newpage.php' page.当我单击“samplepage.php”上的“提交”按钮时,我将页面重定向到“newpage.php”页面。 I would like to reset 'samplepage.php' form values when user clicks on back button of the browser.当用户单击浏览器的后退按钮时,我想重置“samplepage.php”表单值。 Right now it keeps all the textbox values, radio button and checkbox selection.现在它保留了所有的文本框值、单选按钮和复选框选择。 Could you please let me know if there are any suggestions?如果有任何建议,你能告诉我吗? Thanks for your help.谢谢你的帮助。

Here is my redirect php code.这是我的重定向 php 代码。

header ("location:newpage.php);  

The pageshow event is sent to a Window when the browser displays the window's document due to navigation.当浏览器由于导航而显示窗口的文档时,pageshow 事件被发送到窗口。 This includes: Navigating to the page from another page in the same window or tab这包括: 从同一窗口或选项卡中的另一个页面导航到该页面

Example pageshow event and reset form on browser back button浏览器后退按钮上的示例页面显示事件和重置表单

<script>
$(window).bind("pageshow", function() {
    $('#yourformid').trigger("reset");

});
</script>

you can't watch the browser's back button with server-side code.您无法使用服务器端代码观看浏览器的后退按钮。

You need Javascript and jQuery with a little knowledge in the 'popstate' and 'pushstate' arena您需要 Javascript 和 jQuery,并且对“popstate”和“pushstate”领域有一些了解

This is something related to the browser.这与浏览器有关。

You can empty your inputs at last moment (before submitting) and put the values in some hidden fields...您可以在最后一刻(提交之前)清空您的输入并将值放在一些隐藏字段中...

You'll have to clear it on page load if you really want to do this.如果你真的想这样做,你必须在页面加载时清除它。 If you have more than a few controls you'll want to loop through the form to clear it.如果您有多个控件,您将需要遍历表单以清除它。 Assuming they're all items...假设它们都是物品......

var elems = document.getElementsByTagName("input")
for (var i=0;i<elems.length;i++)
{
    elems[i].value = '';
}

It gets all input controls, loops through them all and sets their values to blank.它获取所有输入控件,遍历它们并将它们的值设置为空白。 You can extend this to work for radio buttons/checkboxes by setting .checked depending on their type.您可以通过根据类型设置 .checked 将其扩展为适用于单选按钮/复选框。

Just my two cents on the issue though, it's a bad idea to clear the form.不过,我在这个问题上只花了两美分,清除表格是个坏主意。 There's a reason browsers retain the state they do.浏览器保留它们的状态是有原因的。

For example, if validation failed on server side and I had to go back and re enter everything I would not be happy.例如,如果服务器端验证失败,我不得不返回并重新输入所有内容,我会不高兴。 :( :(

You forgot die()你忘了死()

header ("location:newpage.php");
die();

You can also try resetting your $_POST, $_GET and $_SESSION vars你也可以尝试重置你的 $_POST、$_GET 和 $_SESSION 变量

unset($_POST,$_SESSION, $_GET);
header ("location:newpage.php);
die();

Update : just read you're trying to unset on history back, I didn't realize that in my above code...更新:刚刚读到您正在尝试取消历史记录,我没有意识到在我上面的代码中...

You're going have to write some markup code to clear the variables associated with the input fields and selection lists of the form.您将不得不编写一些标记代码来清除与表单的输入字段和选择列表相关联的变量。

Whatever.js随便.js

function clearForm(formIdent) 
{ 
  var form, elements, i, elm; 
  form = document.getElementById 
    ? document.getElementById(formIdent) 
    : document.forms[formIdent]; 

    if (document.getElementsByTagName)
    {
        elements = form.getElementsByTagName('input');
        for( i=0, elm; elm=elements.item(i++); )
        {
            if (elm.getAttribute('type') == "text")
            {
                elm.value = '';
            }
        }
        elements = form.getElementsByTagName('select');
        for( i=0, elm; elm=elements.item(i++); )
        {
            elm.options.selectedIndex=0;
        }
    }

    // Actually looking through more elements here
    // but the result is the same.
    else
    {
        elements = form.elements;
        for( i=0, elm; elm=elements[i++]; )
        {
            if (elm.type == "text")
            {
                elm.value ='';
            }
        }
    }
}

function addEvent(elm, strEvent, fnHandler)
{
    return ( elm.addEventListener
    ? elm.addEventListener( strEvent, fnHandler, false)
    : elm.attachEvent( 'on'+strEvent, fnHandler)
    );
}

In the Form Div在表单 Div 中

<script type="text/javascript">
  // <![CDATA[
    var el = document.createElement("input");
    el.setAttribute("id", "clearButton");
    el.setAttribute("type", "button");
    el.setAttribute("value", "Clear");
    document.getElementById("FormDiv").appendChild(el);
    addEvent(el, 'click', function(){ clearForm('form_name');} );
  // ]]>
</script>

Alternatively you can also use the HTML event 'onunload' to do the same thing the posters before have suggested.或者,您也可以使用 HTML 事件 'onunload' 来执行海报之前建议的相同操作。 From your perspective it probably does not matter WHEN you fire the event.从您的角度来看,何时触发事件可能无关紧要。

Clear the values on page load.清除页面加载时的值。

<input type="text" id="ClearIt" value=""/>

Place this in the opening body tag:将其放在开头的正文标签中:

onload="document.getElementById('ClearIt').value=''"

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

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