如何使用 Cookie 将信息保存在输入标签中

how to save information in input tags using cookie

本文关键字:存在 输入 标签 保存 信息 何使用 Cookie      更新时间:2023-09-26

我在网页上有一个表格。我想使用 cookie 在输入字段中保存信息。 即我想重新打开我的网页并查看上次打开的网页的数据。

.HTML

                    <table class="table table-striped" id="table-visual-features">
                        <thead>
                            <tr>
                                <th>Visual Feature</th>
                                <th>Step</th>
                                <th>Output</th>
                                <th>Data Feature</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr><td>x</td> 
                                <td><select><option>first</option></select></td>
                                <td><select><option>output</option></select></td>
                                <td><input name="data-feature_x"  id="value0" class = "feature-execution"/></td>
                           </tr>
                            <tr><td>x</td> 
                                <td><select><option>second</option></select></td>
                                <td><select><option>output</option></select></td>
                                <td><input name="data-feature_x"  id="value1" class = "feature-execution"/></td>
                           </tr>

                            <tr><td>x</td> 
                                <td><select><option>third</option></select></td>
                                <td><select><option>output</option></select></td>
                                <td><input name="data-feature_x"  id="value2" class = "feature-execution"/></td>
                           </tr>

                        </tbody>
                    </table>

您可以使用 jQuery Cookie 来设置和检索 Cookie 的内容。

使用 $.cookie.json = true 可以存储 JSON 数据。

代码:

var getValue = function(id){
  return document.querySelector('#'+id).value;
};
var saveCookie = function(){
  document.cookie = '';
  document.cookie += 'a='+getValue('value0');
  document.cookie += 'b='+getValue('value1');
  document.cookie += 'c='+getValue('value2');
};

演示:

http://jsbin.com/IZULuRE/4/edit

当然,根据您的使用:)编辑代码,这是唯一的演示。