使用jquery.cookie.js插件实现记住密码功能

来源:互联网 发布:淘宝a货店铺推荐 编辑:程序博客网 时间:2024/05/25 18:10

1.先导入要用到类库

<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript" src="../jquery.cookie.js"></script>

具体路径要看大家的文件放哪了哈,顺序不能颠倒,要不会出现错误,jquery.cookie.js文件是依赖jquery.js的。

2.jquery代码

</pre><pre class="javascript" name="code"><script type="text/javascript">$(function(){if($.cookie("username")){$("#username").val($.cookie("username"));}if($.cookie("password")){$("#password").val($.cookie("password"));}})function saveCookie(){if($("#chkSave").is(":checked")){$.cookie("username",$("#username").val(),{expires:7})$.cookie("password",$("#password").val(),{expires:7})}else{$.cookie("username",null,{expires:0})$.cookie("password",null,{expires:0})}}</script>


3.表单如下

<form id="form1"> <div> 用户名:<input type="text" id="username" > <input type="password" id="password"> 密码:<input type="checkbox" id="chkSave" />记住密码 <button type="submit" id="submit" value="登录" onClick="saveCookie()"/> </div> </form> 

4.其中判断复选框是否被选中的方法是$("#chkSave").is(":checked") 实测有效。仅供参考

0 0