2016-07-18 24 views
0

こんにちは私は最近htmlで作業を始めましたが、iamはあるHTMLページから次のhtmlページへの値私たちの名前は後javascript変数を1つのhtmlページから別のページに渡す

pageOne.html

<!DOCTYPE html> 
<head> 
<title> 
Home 
</title> 
<meta charset="UTF-8"> 
</head> 
<style> 
.myP{ 
color: lightgreen;} 
</style> 
<body> 
<p id="p1" class="myP" onclick="myFun()">DataSend</p> 
</body> 
<script> 
function myFun(){ 
document.getElementById("p1").style.color = "blue"; 
var textprevi=document.getElementById("p1").innerHTML; 
localStorage.setItem("message", textprevi); 
window.open("pageTwo.html","_self"); 
} 
</script> 
</html> 

、私は2つのHTMLページを書かれているsuccess.Hereをサインアップして、私の第二

pageTwo.html

<!DOCTYPE html> 
<html> 
<body onload="fun"> 
<input type="text" id="tBox"> 
</body> 
<script> 
function fun() 
{ 
document.getElementById("tBox").innerHTML = localStorage.getItem("message"); 
} 

</script> 
</html> 

しかし、私は、上記の溶液をしようとしたとき、ID =「TBOX」の要素は空であったが、私はそれが由来する値=「のdatasend」で充填されたかっpageOne.html。 私の約束を助けてください。 ありがとうございます。

答えて

1

問題は、この行をここで

document.getElementById("tBox").innerHTML = localStorage.getItem("message"); 

tBoxです入力要素。だから、あなたがpageTwo.htmlに2つのミスを犯しinnerHTML

document.getElementById("tBox").value= localStorage.getItem("message"); 
0

あなたがページ1でクエリ文字列パラメータ を使用してそれを行うことができます。

ページで ​​ が
function getParameterByName(name, url) { 
    if (!url) url = window.location.href; 
    name = name.replace(/[\[\]]/g, "\\$&"); 
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), 
     results = regex.exec(url); 
    if (!results) return null; 
    if (!results[2]) return ''; 
    return decodeURIComponent(results[2].replace(/\+/g, " ")); 
} 

var data = getParameterByName('data'); 
0

の代わりにvalueを使用する必要があります。

<!DOCTYPE html> 
<html> 
<body onload="fun()"><!-- You didn't invoke the function here --> 
<input type="text" id="tBox"> 
</body> 
<script> 
function fun() 
{ 
document.getElementById("tBox").value = localStorage.getItem("message"); // You need to set the value for input fields, not innerHTML. 
} 

</script> 
</html> 

これらの変更により、コードが機能するはずです。

関連する問題