关于jquery插件cookies的使用

下面就是一个例子:不过没有设置 domain、path、hoursToLive……几个属性,所以一旦关闭浏览器就不会保存cookie了。

<html>
<head>
<script type="text/javascript" src="jquery.js"></script> //插入js
<script type="text/javascript" src="jquery.cookies.2.2.0.min.js"></script>//插入插件。注意:这个插件一定要放在jquery文件的后面,否则不会生效
<script type="text/javascript">
$(document).ready(function(){
$(".tijiao").click(function(){ //触发button事件
$.cookies.set('name',$(".userName").value);
$.cookies.set('password',$(".password").value); //把表单中得到的value存入cookie
alert("已经存入cookie");
});
var userN=$.cookies.get( 'name' );
var userP=$.cookies.get( 'password' ); //从cookie中读取数据
if(userN!=""&&userN!=null&&userP!=""&&userP!=null)
{$(".userName").value=userN;
$(".password").value=userP; // 赋值
}
else{
alert("userName or password is not exist");};
})
</script>
</head>
<body>
<div>
<table>
<tr id="name">
<td><lable>用户名:</lable></td>
<td><input type="text" class="userName" value=""/></td>
</tr>
<tr>
<td><lable>密码:</lable></td>
<td><input type="password1" class="password" value=""/></td>
</tr>
<tr>
<td><input type="button" class="tijiao" value="登录"/></td>
</tr>
</table>
</div>
</body>
</html>

要想设置cookie的其他属性就看这个:
var newOptions = {
domain: '*.mydomain.com',//设置域
path: '/somedir',//设置路径
expiresAt: new Date( 2011, 1, 1 ),//设置cookie失效时间
hoursToLive:0.01//设置cookie存活时间
secure: true//是否保护
}

详情请查看:
http://code.google.com/p/cookies/wiki/Documentation#Test_if_browser_is_accepting_cookies
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值