我想使用查询字符串将一个变量的值传递给另一个html页面。
first.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>
<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>
正如你的建议我创建了我的页面。这些仍在进行所需的操作。我的错误是什么?
答案 0 :(得分:4)
请解释“所需结果”是什么..
Page 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>
第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>
答案 1 :(得分:2)
将method="get"
添加到form
元素。否则,可能会使用POST
(默认值),它以不同的方式传递参数。