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

问题描述 投票:1回答:1

我必须使用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>");
javascript html5 forms
1个回答
1
投票

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

const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get('myParam');
© www.soinside.com 2019 - 2024. All rights reserved.