如何使用JavaScript将HTML表单数据从一个页面发送到另一个页面
我正在尝试使用Javascript将HTML表单数据从一个页面发送到另一个页面。这是我的密码。假设我正在FORM.html页面的“NAME”字段中输入任何文本。提交后,文本将显示在display.html页面上。怎么做?请帮忙 FORM.html如何使用JavaScript将HTML表单数据从一个页面发送到另一个页面,javascript,html,forms,Javascript,Html,Forms,我正在尝试使用Javascript将HTML表单数据从一个页面发送到另一个页面。这是我的密码。假设我正在FORM.html页面的“NAME”字段中输入任何文本。提交后,文本将显示在display.html页面上。怎么做?请帮忙 FORM.html <html> <head> <title>FORM</title> </head> <body> <form method="GET" action="display.htm
<html>
<head>
<title>FORM</title>
</head>
<body>
<form method="GET" action="display.html">
NAME: <input type="text" name="name">
<input type="submit" value="Submit">
</form>
</body>
</html>
<html>
<head>
<title>Display</title>
</head>
<body>
<p id="show">
Name: <!-- want to display the name here -->
</p>
</body>
</html>
<html>
<head>
<title>FORM</title>
</head>
<body>
<form id="form" method="GET" action="display.html">
NAME: <input type="text" name="name" id="name">
<input type="button" value="Submit" onclick="submitForm()">
</form>
<script>
function submitForm(){
if(typeof(localStorage) != "undefined"){
localStorage.name = document.getElementById("name").value;
}
document.getElementById("form").submit();
}
</script>
</body>
</html>
<html>
<head>
<title>Display</title>
</head>
<body onload="setData()">
<p id="show">
Name: <!-- want to display the name here -->
</p>
<script>
function setData(){
if(typeof(localStorage) != "undefined"){
document.getElementById("show").innerHTML = localStorage.name;
}
}
</script>
</body>
</html>
形式
姓名:
DISPLAY.html
<html>
<head>
<title>FORM</title>
</head>
<body>
<form method="GET" action="display.html">
NAME: <input type="text" name="name">
<input type="submit" value="Submit">
</form>
</body>
</html>
<html>
<head>
<title>Display</title>
</head>
<body>
<p id="show">
Name: <!-- want to display the name here -->
</p>
</body>
</html>
<html>
<head>
<title>FORM</title>
</head>
<body>
<form id="form" method="GET" action="display.html">
NAME: <input type="text" name="name" id="name">
<input type="button" value="Submit" onclick="submitForm()">
</form>
<script>
function submitForm(){
if(typeof(localStorage) != "undefined"){
localStorage.name = document.getElementById("name").value;
}
document.getElementById("form").submit();
}
</script>
</body>
</html>
<html>
<head>
<title>Display</title>
</head>
<body onload="setData()">
<p id="show">
Name: <!-- want to display the name here -->
</p>
<script>
function setData(){
if(typeof(localStorage) != "undefined"){
document.getElementById("show").innerHTML = localStorage.name;
}
}
</script>
</body>
</html>
展示
姓名:
提交FORM.html表单时,将名称保存在url中
当加载DISPLAY.html表单时,可以从url中读取名称,该表单带有在onload页面中运行的javascript函数
您必须将DISPLAY.html替换为以下内容:
<html>
<head>
<title>Display</title>
</head>
<body onload="getName()">
<p id="show">
<div id='myDiv'>Name: <!-- want to display the name here -->
</div>
</p>
</body>
<script type="text/javascript">
function getName()
{
var name = window.location.href.split("?name=")[1].split("+").join(" ");
var fieldNameElement = document.getElementById('myDiv');
var oldText=fieldNameElement.innerHTML;
fieldNameElement.innerHTML = oldText+' '+name;
}
</script>
</html>
展示
姓名:
函数getName()
{
var name=window.location.href.split(“?name=”)[1].split(“+”)。join(“”);
var fieldnamelement=document.getElementById('myDiv');
var oldText=fieldnamelement.innerHTML;
fieldNameElement.innerHTML=oldText+''名称;
}
至少在Chrome上对我有效
如果url中有更多元素,可以使用split并获取“&”之间的元素
当您提交FORM.html表单时,请将名称保存在url中 当加载DISPLAY.html表单时,可以从url中读取名称,该表单带有在onload页面中运行的javascript函数 您必须将DISPLAY.html替换为以下内容:
<html>
<head>
<title>Display</title>
</head>
<body onload="getName()">
<p id="show">
<div id='myDiv'>Name: <!-- want to display the name here -->
</div>
</p>
</body>
<script type="text/javascript">
function getName()
{
var name = window.location.href.split("?name=")[1].split("+").join(" ");
var fieldNameElement = document.getElementById('myDiv');
var oldText=fieldNameElement.innerHTML;
fieldNameElement.innerHTML = oldText+' '+name;
}
</script>
</html>
展示
姓名:
函数getName()
{
var name=window.location.href.split(“?name=”)[1].split(“+”)。join(“”);
var fieldnamelement=document.getElementById('myDiv');
var oldText=fieldnamelement.innerHTML;
fieldNameElement.innerHTML=oldText+''名称;
}
至少在Chrome上对我有效
如果url中有更多元素,可以使用split并获取“&”之间的元素
关于如果您只想通过
JavaScript
完成,那么您可以使用window.localStorage
属性在本地存储名称
对象
Form.html
<html>
<head>
<title>FORM</title>
</head>
<body>
<form method="GET" action="display.html">
NAME: <input type="text" name="name">
<input type="submit" value="Submit">
</form>
</body>
</html>
<html>
<head>
<title>Display</title>
</head>
<body>
<p id="show">
Name: <!-- want to display the name here -->
</p>
</body>
</html>
<html>
<head>
<title>FORM</title>
</head>
<body>
<form id="form" method="GET" action="display.html">
NAME: <input type="text" name="name" id="name">
<input type="button" value="Submit" onclick="submitForm()">
</form>
<script>
function submitForm(){
if(typeof(localStorage) != "undefined"){
localStorage.name = document.getElementById("name").value;
}
document.getElementById("form").submit();
}
</script>
</body>
</html>
<html>
<head>
<title>Display</title>
</head>
<body onload="setData()">
<p id="show">
Name: <!-- want to display the name here -->
</p>
<script>
function setData(){
if(typeof(localStorage) != "undefined"){
document.getElementById("show").innerHTML = localStorage.name;
}
}
</script>
</body>
</html>
形式
姓名:
函数submitForm(){
if(typeof(localStorage)!=“未定义”){
localStorage.name=document.getElementById(“name”).value;
}
document.getElementById(“表单”).submit();
}
Display.html
<html>
<head>
<title>FORM</title>
</head>
<body>
<form method="GET" action="display.html">
NAME: <input type="text" name="name">
<input type="submit" value="Submit">
</form>
</body>
</html>
<html>
<head>
<title>Display</title>
</head>
<body>
<p id="show">
Name: <!-- want to display the name here -->
</p>
</body>
</html>
<html>
<head>
<title>FORM</title>
</head>
<body>
<form id="form" method="GET" action="display.html">
NAME: <input type="text" name="name" id="name">
<input type="button" value="Submit" onclick="submitForm()">
</form>
<script>
function submitForm(){
if(typeof(localStorage) != "undefined"){
localStorage.name = document.getElementById("name").value;
}
document.getElementById("form").submit();
}
</script>
</body>
</html>
<html>
<head>
<title>Display</title>
</head>
<body onload="setData()">
<p id="show">
Name: <!-- want to display the name here -->
</p>
<script>
function setData(){
if(typeof(localStorage) != "undefined"){
document.getElementById("show").innerHTML = localStorage.name;
}
}
</script>
</body>
</html>
展示
姓名:
函数setData(){
if(typeof(localStorage)!=“未定义”){
document.getElementById(“show”).innerHTML=localStorage.name;
}
}
如果只想通过JavaScript
完成,那么可以使用window.localStorage
属性将名称
对象本地存储
Form.html
<html>
<head>
<title>FORM</title>
</head>
<body>
<form method="GET" action="display.html">
NAME: <input type="text" name="name">
<input type="submit" value="Submit">
</form>
</body>
</html>
<html>
<head>
<title>Display</title>
</head>
<body>
<p id="show">
Name: <!-- want to display the name here -->
</p>
</body>
</html>
<html>
<head>
<title>FORM</title>
</head>
<body>
<form id="form" method="GET" action="display.html">
NAME: <input type="text" name="name" id="name">
<input type="button" value="Submit" onclick="submitForm()">
</form>
<script>
function submitForm(){
if(typeof(localStorage) != "undefined"){
localStorage.name = document.getElementById("name").value;
}
document.getElementById("form").submit();
}
</script>
</body>
</html>
<html>
<head>
<title>Display</title>
</head>
<body onload="setData()">
<p id="show">
Name: <!-- want to display the name here -->
</p>
<script>
function setData(){
if(typeof(localStorage) != "undefined"){
document.getElementById("show").innerHTML = localStorage.name;
}
}
</script>
</body>
</html>
形式
姓名:
函数submitForm(){
if(typeof(localStorage)!=“未定义”){
localStorage.name=document.getElementById(“name”).value;
}
document.getElementById(“表单”).submit();
}
Display.html
<html>
<head>
<title>FORM</title>
</head>
<body>
<form method="GET" action="display.html">
NAME: <input type="text" name="name">
<input type="submit" value="Submit">
</form>
</body>
</html>
<html>
<head>
<title>Display</title>
</head>
<body>
<p id="show">
Name: <!-- want to display the name here -->
</p>
</body>
</html>
<html>
<head>
<title>FORM</title>
</head>
<body>
<form id="form" method="GET" action="display.html">
NAME: <input type="text" name="name" id="name">
<input type="button" value="Submit" onclick="submitForm()">
</form>
<script>
function submitForm(){
if(typeof(localStorage) != "undefined"){
localStorage.name = document.getElementById("name").value;
}
document.getElementById("form").submit();
}
</script>
</body>
</html>
<html>
<head>
<title>Display</title>
</head>
<body onload="setData()">
<p id="show">
Name: <!-- want to display the name here -->
</p>
<script>
function setData(){
if(typeof(localStorage) != "undefined"){
document.getElementById("show").innerHTML = localStorage.name;
}
}
</script>
</body>
</html>
展示
姓名:
函数setData(){
if(typeof(localStorage)!=“未定义”){
document.getElementById(“show”).innerHTML=localStorage.name;
}
}
如果不使用PHP等服务器技术,可以使用Javascript设置cookie或使用浏览器的会话存储。但这通常不是表单和应用程序的工作方式。检查:如果你不使用像PHP这样的服务器技术,你可以使用Javascript来设置cookie或使用浏览器的会话存储。但这通常不是表单和应用程序的工作方式。检查:当你可以用一个变量来获得名称时,为什么要用三个变量呢?var name=window.location.href.split(“?name=”)[1];是的,你的解决方案更有效。我用你的编辑我的,谢谢!谢谢此代码正在运行。。但问题是它不能打印空格。假设我进入“丹尼·博伊尔”。它正在显示“Danny+Boyle”。我编辑了我的响应,替换了这一行:var name=window.location.href.split(“?name=”)[1]对于这一行:var name=window.location.href.split(“?name=”)[1]。split(“+”).join(”;此更改将替换“”的所有“+”,如果可以使用一个变量,为什么要使用3个变量来获取名称?var name=window.location.href.split(“?name=”)[1];是的,你的解决方案更有效。我用你的编辑我的,谢谢!谢谢此代码正在运行。。但问题是它不能打印空格。假设我进入“丹尼·博伊尔”。它正在显示“Danny+Boyle”。我编辑了我的响应,替换了这一行:var name=window.location.href.split(“?name=”)[1]对于这一行:var name=window.location.href.split(“?name=”)[1]。split(“+”).join(”;此更改将替换“”的所有“+”谢谢代码。它对我有效,但问题是,当我按下“回车”按钮提交姓名时,它显示的是以前的姓名。未显示新名称。感谢提供代码。它对我有效,但问题是,当我按下“回车”按钮提交姓名时,它显示的是以前的姓名。没有显示新名称。