javascript - 从一个 HTML 页面到另一个 HTML 页面的表单数据
问题描述
我必须使用 Java 脚本将表单数据从一个 HTML 页面 (test.html) 传递到另一个 (test1.html)。不能使用 PHP,因为我们还没有研究过,也不允许这样做。我能够传递和打印数据,但它被打印在同一页面(test.html)上,而我想在 test1.html 上打印它。你能帮我找出问题所在吗?我附上了 HTML 代码和 Javascript。
<body>
<form onsubmit="results();" action="test1.html" method="get">
What is your name?
<input id="f1" type="text" name="name" required /><br />
What is your Father Name?
<input id="f2" type="text" name="fname" required />
Gender:
<select name="gender">
<option id="m" value="Male" selected>Male</option>
<option id="f" value="Female">Female</option>
<option id="o" value="Others">Other</option>
</select>
Date of Birth*: <br />
<input type="date" id="b1" name="bday" required><br /><br />
<input id="submit" type="submit" />
</form>
<script src="./response.js"></script>
</body>
Java 脚本:
function results() {
var name = document.getElementById('f1').value;
var fname = document.getElementById('f2').value;
if (document.getElementById('m').selected) {
gender = document.getElementById('m').value;
}
else if (document.getElementById('f').selected) {
gender = document.getElementById('f').value;
}
else {
gender = document.getElementById('o').value;
}
var dob = document.getElementById('b1').value;
document.write("Here is the Summary of Your Results");
document.write("Your Name Is: ");
document.write(name + "<br/>");
document.write("Your Father Name Is: ");
document.write(fname + "<br/>");
document.write("Your Gender Is: ");
document.write(gender + "</br>");
document.write("Your Date of Birth Is: ");
document.write(dob + "</br>");
解决方案
您应该onsubmit
从表单和 html1.html 文件中删除事件,您可以使用URLSearchParams
这很容易从 url 获取查询参数
const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get('myParam');
推荐阅读
- python - 如何在python脚本中使用进程名杀死一个进程
- java - Maven中2个不同项目中的循环依赖
- scrapy - 亚马逊立即检测到scrapy。如何防止验证码?
- r - R中组的滞后差异
- visual-studio-code - 大写 VSCode 片段的第一个字母
- go - Go 如何打开文件?
- python - 如何扫描一个大列表以查找所有元素是否与第二个较小列表的元素匹配?
- asp.net-core - 嵌套 .NET Core appsettings.json appsettings.development.json 和 appsettings.release.json
- php - '(花式撇号?)在 PHP 中是什么意思
- javascript - .forEach 不是函数节点 JS