Html 如何使用java脚本将值从一个HTML页面传递到另一个页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7269640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass values from one HTML page to other using java script?
提问by Pushpendra Kuntal
I want to pass my value of one variable to another html page using query string.
我想使用查询字符串将一个变量的值传递给另一个 html 页面。
first.html:
第一个.html:
<body >
<form id="form1" name="form1" action="2.html">
<input type="text" name="txtFileName" id="txtFileName"/>
<input type="hidden" name="hid1" value="">
<br><input type="submit" value="Send me your name!" onClick="submitform();">
<br>
</form>
<script type="text/javascript">
function submitform()
{
document.form1.hid1.value="hidden value";
document.form1.submit();
}
</script>
</body>
second.html:
第二个.html:
<html>
<head>
<SCRIPT LANGUAGE="javascript">
function getQueryVariable2(variable) {
var query = window.location.search.substring(1);
document.write(query);
var vars = query.split("&");
document.write("<br />");
document.write(vars);
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
}
</SCRIPT>
</head>
<body>
<h1>Good morning</h1>
<script LANGUAGE="javascript">
document.write("<br />txtFileName = " + getQueryVariable2("txtFileName"));
document.write("<br />hid1 = " + getQueryVariable2("hid1"));
</script>
</body>
</html>
As you suggested I created my pages. Still these are working the desired operation. What is my error?
正如你所建议的,我创建了我的页面。这些仍然在进行所需的操作。我的错误是什么?
回答by inspite
EDIT: I've edited slightly and this seems to work for me, what exactly is the problem?
编辑:我稍微编辑了一下,这似乎对我有用,到底是什么问题?
Please explain what "required result" is..
请解释什么是“要求的结果”。
Page 1
第 1 页
<form id="form1" name="form1" method="get" action="2.html">
<input type="text" name="txtFileName" id="txtFileName"/>
<input type="hidden" name="hid1" value="">
<br><input type="submit" value="Send me your name!" onClick="submitform();">
<br>
</form>
<script type="text/javascript">
function submitform()
{
document.form1.hid1.value="hidden value";
document.form1.submit();
}
</script>
Page 2
第2页
<SCRIPT LANGUAGE="javascript">
function getQueryVariable2(variable) {
var query = window.location.search.substring(1);
document.write(query);
var vars = query.split("&");
document.write("<br />");
document.write(vars);
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
}
document.write("<br />txtFileName = " + getQueryVariable2("txtFileName"));
document.write("<br />hid1 = " + getQueryVariable2("hid1"));
</SCRIPT>
Have a look at this link http://www.htmlgoodies.com/beyond/javascript/article.php/3471111/A-Quick-Tutorial-on-JavaScript-Variable-Passing.htm
回答by Aaron Digulla
Add method="get"
to the form
element. Otherwise, POST
(the default) might be used which passes parameters in a different way.
添加method="get"
到form
元素。否则,POST
可能会使用(默认值)以不同的方式传递参数。
请参阅:FORM 方法属性