2016-12-04 54 views
0

你好,我想從一個HTML5頁面我的輸入保存到一個變種輸入 ,並希望以顯示它的另一頁上..保存輸入並與DOM寫出來 - JavaScript的

在這裏你可以看到我的編碼。

<html> 
 
    <head> 
 
     <title>TODO supply a title</title> 
 
     <meta charset="UTF-8"> 
 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
 
    </head> 
 
    <body> 
 
     <form method ="post" action="TestJS.html"> 
 
      <p>Vorname: <input type="text" id="vorname"></p> 
 
      <p><input value="Send" type="submit"></p> 
 
     </form> 
 
    </body> 
 
</html>

  
 
    
 
<html> 
 
    <head> 
 
     <title>Testing of JavaScript - Language</title> 
 
     <meta charset="UTF-8"> 
 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
 
    </head> 
 
    <body> 
 
<script> 
 
     
 
     var input = document.getElementById('vorname').value; 
 
     document.write(input); 
 
     
 
    </script> 
 
     
 
    </body> 
 
</html>

+0

JavaScript不能訪問發佈的值。使用get或localStorage()。 – Lain

回答

0

你不能從一個頁面只用前端的JavaScript值發送到另一個。你需要一個後端編程語言。在JavaScript中,node.js應該能夠做到。

0

您可以在<form>元素設置method屬性GET,設置formtarget屬性"_blank"。爲<input>元素提供name屬性,以便將查詢字符串傳遞給TestJS.htmldocument,並將其傳遞給新的window

TestJS.html使用load事件window解析location.searchString.prototype.slice()String.prototype.split()檢索通過查詢字符串,然後在TestJS.html設置元素的.innerHTML

<form method="GET" action="TestJS.html" target="_blank"> 
    <p>Vorname: 
     <!-- set `name` attribute at `input` element --> 
     <input type="text" name="text" id="vorname"> 
    </p> 
    <p> 
     <input value="Send" type="submit"> 
    </p> 
    </form> 

TestJS.html

<div id="result"></div> 
    <script> 
    window.onload = function() { 
     // parse query string parameters of `location.search` : `?text=<input>` 
     var props = location.search.slice(1).split("="); 
     document.getElementById("result").innerHTML = props[0] + ":" + props[1]; 
    } 
    </script> 

plnkr http://plnkr.co/edit/Dq3PE4Xhx3pllIHSpwNy?p=preview