Javascript 如何从一个HTML页面获取数据,以便在提交时更新另一个页面
我有两个html文件book.html和receipt.html。我想使用从book.html表单输入的数据来更新receipt.html 当我的表格和收据都在同一页时,我的代码就起作用了。我希望“提交”按钮打开带有收据的新页面。如何仅使用javascript实现这一点 这是我的js代码Javascript 如何从一个HTML页面获取数据,以便在提交时更新另一个页面,javascript,html,Javascript,Html,我有两个html文件book.html和receipt.html。我想使用从book.html表单输入的数据来更新receipt.html 当我的表格和收据都在同一页时,我的代码就起作用了。我希望“提交”按钮打开带有收据的新页面。如何仅使用javascript实现这一点 这是我的js代码 function alertName(){ var name = document.getElementById("name").value; var email = document.getE
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中,然后在需要时检索它
下面是我在一个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>
<!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>
HTML表单
名字:
姓氏:
提交
函数myFunction(){
//检查浏览器支持
if(类型(存储)!=“未定义”){
//贮藏
localStorage.setItem(“lastname”,document.getElementById(“lastname”).value);
localStorage.setItem(“firstname”,document.getElementById(“firstname”).value);
console.log(“存储成功”)
location.href=“C:/test2.html”;
}
}
并在另一个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>
<!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>
HTML表单
名字:
姓氏:
函数myFunction(){
//检查浏览器支持
if(类型(存储)!=“未定义”){
//取回
document.getElementById(“lastname”).value=localStorage.getItem(“lastname”);
document.getElementById(“firstname”).value=localStorage.getItem(“firstname”);
console.log(“已成功检索”)
}
}
希望获得以下帮助:)您可以使用“localStorage”在一个页面中存储数据,然后在不同的页面中使用它。表单提交到哪里?@charlietfl这是我按钮上的代码<代码>提交完全忽略所有表单数据。为未保存的交易创建收据没有意义anywhere@Nusrath我尝试像这样使用本地存储
if(typeof(storage)!=“undefined”){var name=document.getElementById(“name”).value;localStorage.setItem(“name”,name);}函数alertName(){var upd=document.getElementById(“disp name”).innerHTML;upd=localStorage.getItem(name);}
仍然不太支持你。StackOverflow是一个很棒的社区!像魔术一样工作!!!!好的,我会的。我可以将两个html文件上的js代码合并到一个文件中吗?如果你想在一个html中包含两个js文件,为什么要使用两个html文件。只需在单击“显示另一个html文件”时隐藏一页html即可。