How to have form values same on back button click

2019-02-26 03:38发布

问题:

How to have form values same on back button click in IE?

When I click on back button with following

<input type="button" value="Back" onClick="history.go(-1);return true;" class="back-button">

It does not show form values in IE.

i have a form with some input when i submit form go to next page there i have one back button when click on that i go on form page but there is no values on form which i have field this is issue with IE

回答1:

Form values are not usually saved when moving between pages. They could be if your server configuration supports page caching. An alternative however is to save the form values into cookies or the newer html 5 session storage. This can be accomplished by firing a function in the form header known as onsubmit.

I.E.

<form id="[form id]" name="[form name]" action="[some action]" method="[GET or POST]" onsubmit="saveFormValues();">

In this case when the user clicks the submit button to continue, the values are saved into local browser storage (in essence a local cache).

When the back button is clicked, the onload event of the page could check for the values and then re-assign them to the appropriate fields, thus retaining the choices, selections, and input the user previously entered.

To set a HTML 5 session storage variable:

if(typeof(sessionStorage)!=="undefined")
{
    sessionStorage.[Variable Name]=$([Field Name]).val();
}

On return to the page:

if(typeof(sessionStorage)!=="undefined") {
{
    $([Field Name]).val(sessionStorage.[Variable Name]);
}

This last block will retrieve the saved state value. After final submission is complete, make sure you call:

sessionStorage.clear();

This clears the session storage variables and will make the form appear as a new form when the user revisits the page.

Hope this helps.