使用jQuery cookies记住文本框值

使用jQuery cookies记住文本框值,jquery,html,cookies,Jquery,Html,Cookies,我的页面中有一个文本框和提交按钮。当我点击提交按钮时,页面刷新。我想记住刷新后最后输入到文本框中的值。如何使用jQuery实现这一点?我有以下几点 <input type="text" id = "textbox"> <br/> <input type="Submit" value = " Submit " onClick = " javascript:window.location.reload(); "> 您可以使用本地存储进行发布 local

我的页面中有一个文本框和提交按钮。当我点击提交按钮时,页面刷新。我想记住刷新后最后输入到文本框中的值。如何使用jQuery实现这一点?我有以下几点

  <input type="text" id = "textbox"> <br/>
  <input type="Submit" value = " Submit " onClick = " javascript:window.location.reload(); ">


您可以使用
本地存储
进行发布

localStorage.setItem('yourIndex',$('#textbox').val());
现在如果你想取回物品

localStorage.getItem('yourIndex');
所有最新浏览器都支持本地存储。

要保存cookie:

$.cookie(key,value,{ expires: expire }); //expire in days.
要获取cookie,请执行以下操作:

$.cookie(key);
当然,您需要js,您可以从以下网站获得:

当然还有:
onClick:OnSubmit

function OnSubmit (){
    $.cookie("textbox",$("#textbox").val(),{ expires: 1}); //stores cookie for 1 day, names textbox and stores the value
    window.location.reload();
};

$(document).ready(function() {
    var value = $.cookie("textbox");
    $("#textbox").val(value === undefined || value === null ? "" : value); //insert the value stored in cookie "textbox" only if its not null or undefined , if it does insert empty string
});