2017-04-13 195 views
-1

我有一個網頁 - (aboutMe.html)與 - IMG(#profile) - H4(#title) - DIV(#TXT1) - 按鈕(#編輯)。參數傳遞另一個HTML頁面

<form action="editMe.html" method="GET"> 
     <button type="button" onclick="btntest_onclick()"> Edit </button> 
     <img id="profile" src="images/pic/img1.jpg" /> 
     <h3 id="title"> TITLE GOES HERE</h3> 
     <div id="txt1"> 
      <p>Who Am I ?</p> 
     </div> 
</form> 

function btntest_onclick() 
{ 
    var title = document.getElementById('title').value; 
    var txt1=document.getElementById('txt1').value; 
    var url = "editMe.html?title=" + encodeURIComponent(title) + 
       "&txt1="+encodeURIComponent(txt1) ; 
    document.location.href = url; 
} 

當我按一下按鈕,它會打開第二個網頁 - (editMe.html)與 - IMG(#profile) - 文本區域(#title) - 文本區域(#TXT1) - 按鈕(#保存)。

<form action="editMe.html" method="GET" onload="load()"> 
    <img id="profile" src="#" /> 
    <textarea id="title"> </textarea> 
    <textarea id="txt1"> </textarea> 
</form> 

function load() 
{ 
    var url = document.location.href, 
       params = url.split('?')[1].split('&'), 
       data = {}, 
       tmp; 
    for (var i = 0, l = params.length; i < l; i++) 
    { 
     tmp = params[i].split('='); 
     data[tmp[0]] = tmp[1]; 
    } 
    document.getElementById('title').innerHTML = data.title; 
    document.getElementById('txt1').innerHTML = data.txt1; 
} 

當第二個頁面加載,它應該得到的參數標題,從aboutMe.html txt1中值。但是這個代碼不起作用。請更正上面的代碼

+1

請添加代碼片段,並闡述你到底想要什麼? – Phantom

回答

0

你有幾種情況:

  1. 嘗試合併兩個HTML成一個。
  2. 傳遞您的內容&圖像paratemers通過http請求,如editMe.html?image=IMAGE_URL&title=TITILE_CONTENT
  3. 將值存儲到瀏覽器存儲:cookie或localStorage。
  4. 將值存儲到後端服務器。
0

我已經通過這個代碼解決它:

aboutMe.html

<form action="editMe.html" method="GET"> 
    <button type="button" onclick="btntest_onclick()"> Edit </button> 
    <img id="profile" src="images/pic/img1.jpg" /> 
    <h3 id="title"> TITLE GOES HERE</h3> 
    <div id="txt1"> 
     <p>Who Am I ?</p> 
    </div> 
</form> 

的javascript:

function btntest_onclick() 
{ 
    var profile=document.getElementById("profile").src; 
    var title = document.getElementById("title").innerHTML; 
    var txt1=document.getElementById("txt1").innerHTML; 

    var url = "editMe.html?profile=" + encodeURIComponent(profile)+ 
         "&title=" + encodeURIComponent(title) + 
         "&txt1="+encodeURIComponent(txt1) ; 
    document.location.href = url; 
} 

** ** editMe.html

<body onload="load()"> 
    <form action="editMe.html" method="GET"> 
     <img id="profile" src="#" /> 
     <textarea id="title"> </textarea> 
     <textarea id="txt1"> </textarea> 
    </form> 
</body> 

的javascript:

function load() 
{ 
    var url = document.location.href, 
     params = url.split('?')[1].split('&'), 
     data = {}, 
     tmp; 
    for (var i = 0, l = params.length; i < l; i++) 
    { 
     tmp = params[i].split('='); 
     data[tmp[0]] = tmp[1]; 
    } 
    document.getElementById("profile").src=decodeURIComponent(data.profile); 
    document.getElementById("title").innerHTML = decodeURIComponent(data.title); 
    document.getElementById('txt1').innerHTML = decodeURIComponent(data.txt1); 
} 
相關問題