Remember values from form field for a different pa

2019-08-13 20:00发布

问题:

I have "Page 1" and "Page 2".

On "Page 1" if I click on the button "Go To Page 2" I will get to "Page 2", also on "Page 1" I have a input field "This field right here" as placeholder.

On "Page 2" I have a button "Go Back To Page 1" that when I click it, it will bring me to "Page 1".

What I want is, after I click "Go Back To Page 1" to autocomplete the input "This field right here" with the submited value before I click on "Go To Page 2".

Bellow you have my current code:

Page 1

<script type="text/javascript">
    function getRandomPage () {
    document.getElementById('MyButton').onclick = location.href = "/?random=1";      
        };
</script>

<form>
<div class="checkboxes" align="center">
        <input type="hidden" name="random" value="1"><br>
        <input placeholder="This field right here" style="width: 296px; height: 30px;" type="text" autocomplete="off"><br>
        <label for="x"><input type="checkbox" id="x" checked=""><span> Some Text </span></label><br>
        <label for="y"><input type="checkbox" id="y" disabled=""><span> Some Text </span></label><br>
        <button id="MyButton" type="submit">Go To Page 2</button>
    </div>
</form>

Page 2

<form action="/">
    <div align="center">
        <button type="submit">Go Back To Page 1</button>
    </div>
</form>

I'm thinking maybe it could be done with js maybe? But I don't know how

回答1:

You can use local storage from HTML5, like below:

// Store
localStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname"); 


回答2:

To remember values you can use :

  • HTML5 Storage like localStorage or SessionStorage. https://developer.mozilla.org/en-US/docs/DOM/Storage

  • cookie : https://developer.mozilla.org/en-US/docs/DOM/document.cookie

Use like that :

localStorage.variable = "saveMe";