javascript 将捕获的数据从带有变量的函数传输到名为“user”的单个对象,并将数据保存在sessionStorage中

gijlo24d  于 2023-01-11  发布在  Java
关注(0)|答案(1)|浏览(85)
<div id="body-container">
    <form id="user" action="login.html">
        <input type="text" id="firstname" placeholder="Enter your First Name">
        <input type="text" id="lastname" placeholder="Enter your Last Name">
        <input type="number" id="age" placeholder="Enter your Age">
            <select id="gender"" name="gender">
                <option value="Male">Male</option>
                <option value="Female">Female</option>
            </select>
        <input type="text" id="username" placeholder="Enter your Username">
        <input type="password" id="password" placeholder="Enter your Password">
        <br> <br>
        <input type="submit" value="Submit" onclick="register()">
    </form> 
</div>

<footer id="footer">
    <p>Created on November 2019, by <a href = "http://Jon_Doe.com>John Doe</a></p>
</footer>

<script>
     function register(){
         var firstname = document.getElementById("firstname").value
        var lastname = document.getElementById("lastname").value
        var age = document.getElementById("age").value
        var gender = document.getElementById("gender").value
    }
</script>

我必须从函数register()传输所有捕获的数据,并将其保存在名为“user”的对象中,然后将该数据保存到sessionStorage。

enxuqcxy

enxuqcxy1#

您可以使用以下数据创建对象:

function register(){
    var firstname = document.getElementById("firstname").value;
    var lastname = document.getElementById("lastname").value;
    var age = document.getElementById("age").value;
    var gender = document.getElementById("gender").value;

    var user = {
        firstname: firstname,
        lastname: lastname,
        age: age,
        gender: gender,
    };
}

并使用sessionStorage.setItem("user", JSON.stringify(user))存储

相关问题