2016-05-26 14 views
3

page2入力のpage1に入力値を表示する方法は? Javascriptをpage2入力のpage1に入力値を表示する方法は?

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

} 
</script> 

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> 

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

はい、 'localStorage'はページでは、仕事をする1' localStorage.setItem( 'ID'、のdocument.getElementById( "USER_INPUT")値。);ページ2 'でその後の' – Satpal

+0

私はまだ関数showInput()を入れる必要がありますか – BEX

答えて

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> 
+0

それは働いています!どうもありがとうございます。 :)私はページをリフレッシュした後だけ値を表示します。ボタンをクリックした後に、どうすれば表示させることができますか? – BEX

+0

しかし、2ページあります...そのような場合にはpage1.htmlとpage2.htmlをリフレッシュせずにどうすればいいですか –

0

この

Page1の

<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> 
関連する問題