如何在page2输入的page1中显示输入值?

时间:2016-05-26 06:19:14

标签: javascript jquery html input local-storage

如何在第2页输入的page1中显示输入值? 的Javascript

<script language="JavaScript">
function showInput() {
    var value =  document.getElementById("user_input").value;  
    document.getElementById('display').value = value;

}
</script>

Page 1 html

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
</head>
<body>

    <form>
        <label><b>Enter a Message</b></label>
        <input type="text" name="message" id="user_input">
    </form>

    <input type="submit" onclick="showInput();"><br />
</body>
</html>

Page 2 html

<html>
<head></head>
<body>
    <label>Your input: </label>
    <p><input id='display'> </p>
</body>
</html>

我需要使用本地存储吗?如何使用它? 把它放在第1页

localStorage.setItem(id, value);

,这在第2页?

localStorage.getItem(id);

如何在输入中显示值? 有人能告诉我一些例子吗?谢谢。

2 个答案:

答案 0 :(得分:3)

试试这个:

page1.html

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<script language="JavaScript">
function showInput() {
    var value =  document.getElementById("user_input").value;   // get value of message
    localStorage.setItem('val',value); // set value of message in local storage
}
</script>
</head>
<body>

    <form action="page2.html" method="post"> // add action and method attributes in form tag
        <label><b>Enter a Message</b></label>
        <input type="text" name="message" id="user_input">
        <input type="submit" onclick="showInput();"><br />
    </form>
</body>
</html>

page2.html

<html>
<head>
<script language="JavaScript">
function showInput() {
    var value = localStorage.getItem('val'); // get value of message from localstorage
    document.getElementById('display').value = value; // set it value to display input
}
</script>
</head>
<body onload="showInput();">
    <label>Your input: </label>
    <p><input id='display'> </p>
</body>
</html>

答案 1 :(得分:0)

试试这个

第1页

<script>
   var value =  document.getElementById("user_input").value;
   localStorage.setItem("id",value);
</script>

2页

<script>
    var value =localStorage.getItem("id");
    document.getElementById('display').value = value;
</script>