[英]How do I pass form data from one HTML page to another HTML page using ONLY javascript?
[英]How do I get data from One HTML page to update the Another page on submit
我有兩個 html 文件 book.html 和receipt.html。 我想使用從 book.html 表單輸入的數據來更新receipt.html。
當我在同一頁面中同時擁有表單和收據時,我的代碼就起作用了。 我希望提交按鈕打開一個帶有收據的新頁面。 我如何只使用 javascript 來做到這一點。
這是我的js代碼
function alertName(){
var name = document.getElementById("name").value;
var email = document.getElementById("email").value;
var destination = document.getElementById("destination");
var realD= destination.options[destination.selectedIndex].text;
var dat = document.getElementById("date").value;
var location = document.getElementById("locate");
var realL= location.options[location.selectedIndex].text;
document.getElementById("disp-name").innerHTML= name;
document.getElementById("disp-date").innerHTML= dat;
document.getElementById("disp-location").innerHTML= realL;
document.getElementById("disp-destination").innerHTML= realD;
document.getElementById("disp-email").innerHTML= email;
}
document.getElementById('button').addEventListener('click', alertName);
了解有關LocalStorage 的更多信息
我將值存儲在 LocalStorage 中,稍后在需要時檢索它。
下面是我在一個 HTML 頁面中存儲數據的代碼:
<!DOCTYPE html>
<html>
<body>
<h2>HTML Forms</h2>
First name:<br>
<input type="text" name="firstname" id="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" id="lastname" value="Mouse">
<br><br>
<button onclick="myFunction()">Submit</button>
<script>
function myFunction() {
// Check browser support
if (typeof(Storage) !== "undefined") {
// Store
localStorage.setItem("lastname", document.getElementById("lastname").value);
localStorage.setItem("firstname", document.getElementById("firstname").value);
console.log("Storaged successfully")
location.href = "C:/test2.html";
}
}
</script>
</body>
</html>
並在另一個 HTML 頁面中檢索它:
<!DOCTYPE html>
<html>
<body onload="myFunction()">
<h2>HTML Forms</h2>
First name:<br>
<input type="text" name="firstname" id="firstname">
<br>
Last name:<br>
<input type="text" name="lastname" id="lastname">
<br><br>
<script>
function myFunction() {
// Check browser support
if (typeof(Storage) !== "undefined") {
// Retrieve
document.getElementById("lastname").value = localStorage.getItem("lastname");
document.getElementById("firstname").value = localStorage.getItem("firstname");
console.log("Retrieved successfully")
}
}
</script>
</body>
</html>
希望這有幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.