使用jQuery cookie记住文本框值 [英] Remember text box value using jQuery cookies

查看:100
本文介绍了使用jQuery cookie记住文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,并在我的页面提交按钮。当我点击提交按钮,页面刷新。我想记住刷新后最后输入的值到文本框。我怎么能这样使用jQuery?我有以下



 < input type =textid =textbox> < br /> 
< input type =Submitvalue =SubmitonClick =javascript:window.location.reload();>


解决方案

可储存Cookie:

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

可获取Cookie:

  $。cookie(key); 

当然你需要js,你可以从:



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

  function OnSubmit(){
$ .cookie(textbox,$(#textbox)。val {expires:1}); //存储cookie为1天,命名文本框并存储值
window.location.reload();
};

$(document).ready(function(){
var value = $ .cookie(textbox);
$(#textbox)。 === undefined || value === null?:value); //插入存储在cookietextbox中的值,如果它不为null或未定义,如果它插入空字符串
});

p>

I have a text box and submit button in my page. When I click on Submit button, the page refreshes. I want to remember the last entered value to the text box after refresh. How can I do that using jQuery? I have the following

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

解决方案

to save cookie:

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

to get cookie :

$.cookie(key);

of course you need to js, which you can get from :

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

And ofcourse : onClick:OnSubmit

这篇关于使用jQuery cookie记住文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆