繁体   English   中英

从一个 HTML 页面到另一个页面的表单数据

[英]Form data from one HTML page to another

我必须使用 Java Script 将表单数据从一个 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>");

您应该从表单和 html1.html 文件中删除onsubmit事件,您可以使用URLSearchParams这很容易从 url 获取查询参数

const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get('myParam');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM