2014-01-21 23 views
0

我有一個文本框並在我的頁面中提交按鈕。當我點擊提交按鈕時,頁面刷新。我想記住刷新後最後輸入的文本框的值。我怎樣才能使用jQuery?我有以下記住使用jQuery cookies的文本框值

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

回答

0

你可以使用local storage的問題

localStorage.setItem('yourIndex',$('#textbox').val()); 

,現在如果你想檢索項

localStorage.getItem('yourIndex'); 

local storage被所有最新的瀏覽器支持。

0

保存的Cookie:

$.cookie(key,value,{ expires: expire }); //expire in days. 

得到的cookie:

$.cookie(key); 
當然

需要JS,你可以得到:

http://plugins.jquery.com/cookie/

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 
}); 

而且se:onClick:OnSubmit