JQuery实现下次自动登录

Jquery将用户名密码存储到cookie中

需要导入jquery.js和jquery.cookie.js

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<html>
<head>
<title>test cookie</title>
<script src= "Scripts/jquery-1.4.1.min.js" type= "text/javascript" ></script>
<script src= "Scripts/jquery.cookie.js" type= "text/javascript" ></script>
<script type= "text/javascript" >
 
   $(document).ready( function () {
     if ($.cookie( "rmbUser" ) == "true" ) {
     $( "#ck_rmbUser" ).attr( "checked" , true );
     $( "#txt_username" ).val($.cookie( "username" ));
     $( "#txt_password" ).val($.cookie( "password" ));
     }
   });
 
   //记住用户名密码
   function Save() {
     if ($( "#ck_rmbUser" ).attr( "checked" )) {
       var str_username = $( "#txt_username" ).val();
       var str_password = $( "#txt_password" ).val();
       $.cookie( "rmbUser" , "true" , { expires: 7 }); //存储一个带7天期限的cookie
       $.cookie( "username" , str_username, { expires: 7 });
       $.cookie( "password" , str_password, { expires: 7 });
     }
     else {
       $.cookie( "rmbUser" , "false" , { expire: -1 });
       $.cookie( "username" , "" , { expires: -1 });
       $.cookie( "password" , "" , { expires: -1 });
     }
   };
</script>
</head>
<body>
   <div>
     用户名:<input type= "text" id= "txt_username" /><br />
     密码:<input type= "text" id= "txt_password" /><br />
     <input type= "checkbox" id= "ck_rmbUser" />记住用户名和密码<br />
     <input type= "submit" id= "sub" value= "登录" onclick= "Save()" />
   </div>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值