Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript的浏览器存储_Javascript_Jquery_Html_Css - Fatal编程技术网

使用javascript的浏览器存储

使用javascript的浏览器存储,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我试图创建一个表单,因此当用户填写表单时,所有内容都存储在浏览器存储中 但是,我目前想知道如何从页面上的控件获取值,并使用与response.html文件相同的键名将它们存储在会话存储中 我当前得到的输出在每个元素上都是未定义的 “严格使用”; var$=函数(id){return document.getElementById(id);}; var saveReservation=函数(){ //提交表格 $(“预订表”)。提交(); }; window.onload=函数(){ $(“提交

我试图创建一个表单,因此当用户填写表单时,所有内容都存储在浏览器存储中

但是,我目前想知道如何从页面上的控件获取值,并使用与response.html文件相同的键名将它们存储在会话存储中

我当前得到的输出在每个元素上都是未定义的

“严格使用”;
var$=函数(id){return document.getElementById(id);};
var saveReservation=函数(){
//提交表格
$(“预订表”)。提交();
};
window.onload=函数(){
$(“提交请求”).onclick=saveReservation;
$(“到达日期”).focus();
};
正文{
字体系列:Arial、Helvetica、无衬线字体;
背景色:白色;
保证金:0自动;
宽度:600px;
边框:3件纯蓝;
填充:10px 20px;
}
字段集{
边缘顶部:1米;
边缘底部:1米;
填充:.5em;
}
传奇{
颜色:蓝色;
字体大小:粗体;
字号:85%;
边缘底部:.5em;
}
标签{
浮动:左;
宽度:90px;
}
输入,选择{
左边距:1米;
右边距:1米;
边缘底部:.5em;
}
输入{
宽度:14em;
}
输入[type=“radio”],输入[type=“checkbox”]{
宽度:1米;
左侧填充:0;
右边距:0.5em;
}

预订请求
预订请求
一般资料
抵达日期:

晚上:
成人: 1. 2. 3. 4.
儿童: 0 1. 2. 3. 4.
偏好 房间类型: 标准 生意 套房
床型: 国王 双倍双倍
吸烟
联系方式 姓名:
电邮:
电话:


语法似乎有误。试试这些吧

要保存数据/初始化会话存储,请执行以下操作:

// Save data to sessionStorage
sessionStorage.setItem('key', 'value');
从会话存储中获取保存的值

// Get saved data from sessionStorage
sessionStorage.getItem('key');

语法似乎有误。试试这些吧

要保存数据/初始化会话存储,请执行以下操作:

// Save data to sessionStorage
sessionStorage.setItem('key', 'value');
从会话存储中获取保存的值

// Get saved data from sessionStorage
sessionStorage.getItem('key');

据我所知,您正在寻找的是接受用户数据并在另一个HTML中显示它

可以通过使用表单数据创建JSON并将其存储在本地存储器中来实现这一点。您可以返回该表单并将其填充到另一个页面。如果不允许编辑,可以使用只读属性。(或者简单地将字段显示在标签/span/段落等中)

代码如下所示:

registration.html

HTML

<body>
    <main>
    <h1>Reservation Request</h1>
    <form action="response.html" method="post"
        name="form" id="form">
            <fieldset>
            <legend>General Information</legend>
            <label for="arrival_date">Arrival date:</label>
            <input type="text" name="arrival_date" id="arrival_date"><br>
            <label for="nights">Nights:</label>
            <input type="text" name="nights" id="nights"><br>
            <label>Adults:</label>
            <select name="adults" id="adults">
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>                                                
            </select><br>
            <label>Children:</label>
            <select name="children" id="children">
                    <option value="0">0</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>                                                
            </select><br>           
            </fieldset>
            <fieldset>
            <legend>Preferences</legend>
            <label>Room type:</label>
        <input type="radio" name="room" id="standard" value="standard" checked>Standard             
        <input type="radio" name="room" id="business" value="business">Business
        <input type="radio" name="room" id="suite" value="suite">Suite<br>

            <label>Bed type:</label>
        <input type="radio" name="bed" id="king" value="king" checked>King
        <input type="radio" name="bed" id="double" value="double">Double Double<br>
        <input type="checkbox" name="smoking" id="smoking" value="smoking">Smoking<br>
            </fieldset>     
            <fieldset>
            <legend>Contact Information</legend>
            <label for="name">Name:</label>
        <input type="text" name="name" id="name"><br>
            <label for="email">Email:</label>
            <input type="text" name="email" id="email"><br>
        <label for="phone">Phone:</label>
        <input type="text" name="phone" id="phone"><br>
            </fieldset>

            <input type="submit" value="SubmitReservation"><br>
    </form>
    </main>
</body>
<body>
    <main>
    <h1>Reservation Request</h1>
    <form action="response.html" method="post"
        name="form" id="form">
            <fieldset>
            <legend>General Information</legend>
            <label for="arrival_date">Arrival date:</label>
            <input type="text" name="arrival_date" id="arrival_date"><br>
            <label for="nights">Nights:</label>
            <input type="text" name="nights" id="nights"><br>
            <label>Adults:</label>
            <select name="adults" id="adults">
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>                                                
            </select><br>
            <label>Children:</label>
            <select name="children" id="children">
                    <option value="0">0</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>                                                
            </select><br>           
            </fieldset>
            <fieldset>
            <legend>Preferences</legend>
            <label>Room type:</label>
        <input type="radio" name="room" id="standard" value="standard" checked>Standard             
        <input type="radio" name="room" id="business" value="business">Business
        <input type="radio" name="room" id="suite" value="suite">Suite<br>

            <label>Bed type:</label>
        <input type="radio" name="bed" id="king" value="king" checked>King
        <input type="radio" name="bed" id="double" value="double">Double Double<br>
        <input type="checkbox" name="smoking" id="smoking" value="smoking">Smoking<br>
            </fieldset>     
            <fieldset>
            <legend>Contact Information</legend>
            <label for="name">Name:</label>
        <input type="text" name="name" id="name"><br>
            <label for="email">Email:</label>
            <input type="text" name="email" id="email"><br>
        <label for="phone">Phone:</label>
        <input type="text" name="phone" id="phone"><br>
            </fieldset>

            <input type="submit" value="SubmitReservation"><br>
    </form>
    </main>
</body>
response.html

HTML

<body>
    <main>
    <h1>Reservation Request</h1>
    <form action="response.html" method="post"
        name="form" id="form">
            <fieldset>
            <legend>General Information</legend>
            <label for="arrival_date">Arrival date:</label>
            <input type="text" name="arrival_date" id="arrival_date"><br>
            <label for="nights">Nights:</label>
            <input type="text" name="nights" id="nights"><br>
            <label>Adults:</label>
            <select name="adults" id="adults">
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>                                                
            </select><br>
            <label>Children:</label>
            <select name="children" id="children">
                    <option value="0">0</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>                                                
            </select><br>           
            </fieldset>
            <fieldset>
            <legend>Preferences</legend>
            <label>Room type:</label>
        <input type="radio" name="room" id="standard" value="standard" checked>Standard             
        <input type="radio" name="room" id="business" value="business">Business
        <input type="radio" name="room" id="suite" value="suite">Suite<br>

            <label>Bed type:</label>
        <input type="radio" name="bed" id="king" value="king" checked>King
        <input type="radio" name="bed" id="double" value="double">Double Double<br>
        <input type="checkbox" name="smoking" id="smoking" value="smoking">Smoking<br>
            </fieldset>     
            <fieldset>
            <legend>Contact Information</legend>
            <label for="name">Name:</label>
        <input type="text" name="name" id="name"><br>
            <label for="email">Email:</label>
            <input type="text" name="email" id="email"><br>
        <label for="phone">Phone:</label>
        <input type="text" name="phone" id="phone"><br>
            </fieldset>

            <input type="submit" value="SubmitReservation"><br>
    </form>
    </main>
</body>
<body>
    <main>
    <h1>Reservation Request</h1>
    <form action="response.html" method="post"
        name="form" id="form">
            <fieldset>
            <legend>General Information</legend>
            <label for="arrival_date">Arrival date:</label>
            <input type="text" name="arrival_date" id="arrival_date"><br>
            <label for="nights">Nights:</label>
            <input type="text" name="nights" id="nights"><br>
            <label>Adults:</label>
            <select name="adults" id="adults">
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>                                                
            </select><br>
            <label>Children:</label>
            <select name="children" id="children">
                    <option value="0">0</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>                                                
            </select><br>           
            </fieldset>
            <fieldset>
            <legend>Preferences</legend>
            <label>Room type:</label>
        <input type="radio" name="room" id="standard" value="standard" checked>Standard             
        <input type="radio" name="room" id="business" value="business">Business
        <input type="radio" name="room" id="suite" value="suite">Suite<br>

            <label>Bed type:</label>
        <input type="radio" name="bed" id="king" value="king" checked>King
        <input type="radio" name="bed" id="double" value="double">Double Double<br>
        <input type="checkbox" name="smoking" id="smoking" value="smoking">Smoking<br>
            </fieldset>     
            <fieldset>
            <legend>Contact Information</legend>
            <label for="name">Name:</label>
        <input type="text" name="name" id="name"><br>
            <label for="email">Email:</label>
            <input type="text" name="email" id="email"><br>
        <label for="phone">Phone:</label>
        <input type="text" name="phone" id="phone"><br>
            </fieldset>

            <input type="submit" value="SubmitReservation"><br>
    </form>
    </main>
</body>
注意:两个文件(reservation.CSS)的CSS与您的相同。

您可以查看以下JSFIDLE来执行上述代码:

按可在Google chrome中查看本地存储内容
F12->Application tab->local storage在左侧导航栏上

据我所知,您正在寻找的是接受用户数据并将其显示在另一个HTML中

可以通过使用表单数据创建JSON并将其存储在本地存储器中来实现这一点。您可以返回该表单并将其填充到另一个页面。如果不允许编辑,可以使用只读属性。(或者简单地将字段显示在标签/span/段落等中)

代码如下所示:

registration.html

HTML

<body>
    <main>
    <h1>Reservation Request</h1>
    <form action="response.html" method="post"
        name="form" id="form">
            <fieldset>
            <legend>General Information</legend>
            <label for="arrival_date">Arrival date:</label>
            <input type="text" name="arrival_date" id="arrival_date"><br>
            <label for="nights">Nights:</label>
            <input type="text" name="nights" id="nights"><br>
            <label>Adults:</label>
            <select name="adults" id="adults">
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>                                                
            </select><br>
            <label>Children:</label>
            <select name="children" id="children">
                    <option value="0">0</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>                                                
            </select><br>           
            </fieldset>
            <fieldset>
            <legend>Preferences</legend>
            <label>Room type:</label>
        <input type="radio" name="room" id="standard" value="standard" checked>Standard             
        <input type="radio" name="room" id="business" value="business">Business
        <input type="radio" name="room" id="suite" value="suite">Suite<br>

            <label>Bed type:</label>
        <input type="radio" name="bed" id="king" value="king" checked>King
        <input type="radio" name="bed" id="double" value="double">Double Double<br>
        <input type="checkbox" name="smoking" id="smoking" value="smoking">Smoking<br>
            </fieldset>     
            <fieldset>
            <legend>Contact Information</legend>
            <label for="name">Name:</label>
        <input type="text" name="name" id="name"><br>
            <label for="email">Email:</label>
            <input type="text" name="email" id="email"><br>
        <label for="phone">Phone:</label>
        <input type="text" name="phone" id="phone"><br>
            </fieldset>

            <input type="submit" value="SubmitReservation"><br>
    </form>
    </main>
</body>
<body>
    <main>
    <h1>Reservation Request</h1>
    <form action="response.html" method="post"
        name="form" id="form">
            <fieldset>
            <legend>General Information</legend>
            <label for="arrival_date">Arrival date:</label>
            <input type="text" name="arrival_date" id="arrival_date"><br>
            <label for="nights">Nights:</label>
            <input type="text" name="nights" id="nights"><br>
            <label>Adults:</label>
            <select name="adults" id="adults">
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>                                                
            </select><br>
            <label>Children:</label>
            <select name="children" id="children">
                    <option value="0">0</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>                                                
            </select><br>           
            </fieldset>
            <fieldset>
            <legend>Preferences</legend>
            <label>Room type:</label>
        <input type="radio" name="room" id="standard" value="standard" checked>Standard             
        <input type="radio" name="room" id="business" value="business">Business
        <input type="radio" name="room" id="suite" value="suite">Suite<br>

            <label>Bed type:</label>
        <input type="radio" name="bed" id="king" value="king" checked>King
        <input type="radio" name="bed" id="double" value="double">Double Double<br>
        <input type="checkbox" name="smoking" id="smoking" value="smoking">Smoking<br>
            </fieldset>     
            <fieldset>
            <legend>Contact Information</legend>
            <label for="name">Name:</label>
        <input type="text" name="name" id="name"><br>
            <label for="email">Email:</label>
            <input type="text" name="email" id="email"><br>
        <label for="phone">Phone:</label>
        <input type="text" name="phone" id="phone"><br>
            </fieldset>

            <input type="submit" value="SubmitReservation"><br>
    </form>
    </main>
</body>
response.html

HTML

<body>
    <main>
    <h1>Reservation Request</h1>
    <form action="response.html" method="post"
        name="form" id="form">
            <fieldset>
            <legend>General Information</legend>
            <label for="arrival_date">Arrival date:</label>
            <input type="text" name="arrival_date" id="arrival_date"><br>
            <label for="nights">Nights:</label>
            <input type="text" name="nights" id="nights"><br>
            <label>Adults:</label>
            <select name="adults" id="adults">
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>                                                
            </select><br>
            <label>Children:</label>
            <select name="children" id="children">
                    <option value="0">0</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>                                                
            </select><br>           
            </fieldset>
            <fieldset>
            <legend>Preferences</legend>
            <label>Room type:</label>
        <input type="radio" name="room" id="standard" value="standard" checked>Standard             
        <input type="radio" name="room" id="business" value="business">Business
        <input type="radio" name="room" id="suite" value="suite">Suite<br>

            <label>Bed type:</label>
        <input type="radio" name="bed" id="king" value="king" checked>King
        <input type="radio" name="bed" id="double" value="double">Double Double<br>
        <input type="checkbox" name="smoking" id="smoking" value="smoking">Smoking<br>
            </fieldset>     
            <fieldset>
            <legend>Contact Information</legend>
            <label for="name">Name:</label>
        <input type="text" name="name" id="name"><br>
            <label for="email">Email:</label>
            <input type="text" name="email" id="email"><br>
        <label for="phone">Phone:</label>
        <input type="text" name="phone" id="phone"><br>
            </fieldset>

            <input type="submit" value="SubmitReservation"><br>
    </form>
    </main>
</body>
<body>
    <main>
    <h1>Reservation Request</h1>
    <form action="response.html" method="post"
        name="form" id="form">
            <fieldset>
            <legend>General Information</legend>
            <label for="arrival_date">Arrival date:</label>
            <input type="text" name="arrival_date" id="arrival_date"><br>
            <label for="nights">Nights:</label>
            <input type="text" name="nights" id="nights"><br>
            <label>Adults:</label>
            <select name="adults" id="adults">
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>                                                
            </select><br>
            <label>Children:</label>
            <select name="children" id="children">
                    <option value="0">0</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>                                                
            </select><br>           
            </fieldset>
            <fieldset>
            <legend>Preferences</legend>
            <label>Room type:</label>
        <input type="radio" name="room" id="standard" value="standard" checked>Standard             
        <input type="radio" name="room" id="business" value="business">Business
        <input type="radio" name="room" id="suite" value="suite">Suite<br>

            <label>Bed type:</label>
        <input type="radio" name="bed" id="king" value="king" checked>King
        <input type="radio" name="bed" id="double" value="double">Double Double<br>
        <input type="checkbox" name="smoking" id="smoking" value="smoking">Smoking<br>
            </fieldset>     
            <fieldset>
            <legend>Contact Information</legend>
            <label for="name">Name:</label>
        <input type="text" name="name" id="name"><br>
            <label for="email">Email:</label>
            <input type="text" name="email" id="email"><br>
        <label for="phone">Phone:</label>
        <input type="text" name="phone" id="phone"><br>
            </fieldset>

            <input type="submit" value="SubmitReservation"><br>
    </form>
    </main>
</body>
注意:两个文件(reservation.CSS)的CSS与您的相同。

您可以查看以下JSFIDLE来执行上述代码:

按可在Google chrome中查看本地存储内容

F12->应用程序选项卡->左侧导航栏上的本地存储

会话存储设置在哪里?@guest271314什么意思?您最初在
javascript
中将
会话存储设置在哪里?您所做的似乎并不是真正存储数据。您可以创建json并将其存储在会话存储中,然后将其填充回response.html。但是你必须做的是formData->serializedJson->store。GetItem->反序列化JSON->显示。如果您确实愿意,我可以发布所需的代码。@Venky请发布,我还想看看json版本。这里是
sessionStorage
set?@guest271314您的意思是什么?您最初在
javascript
处将
sessionStorage
设置在哪里?您所做的似乎并不是在真正存储数据。您可以创建json并将其存储在会话存储中,然后将其填充回response.html。但是你必须做的是formData->serializedJson->store。GetItem->反序列化JSON->显示。如果您确实愿意,我可以发布所需的代码。@Venky请发布,我想查看json版本alsoNote,点表示法可以获得一个set
sessionStorage
属性。这会初始化会话存储吗@guest271314@steven对如果希望从
sessionStorage
获取项目,则需要在
sessionStorage
中设置项目。setItem('key','value')
用于初始化一个新项目,而
sessionStorage。getItem('key')
用于从该存储中获取项目。它仍然表示未定义;我需要某种形式的if语句来为每个元素重复吗?注意,dot不是