1.下载引入js
下载:http://plugins.jquery.com/cookie/
<script type=
"text/javascript"
src=
"js/jquery.min.js"
></script>
<script type=
"text/javascript"
src=
"js/jquery.cookie.js"
></script>
<body>
<div>
用户名:<input type="text" id="txt_username"/><br />
密码:<input id="txt_password" type="password"/><br />
<input type="checkbox" id="ck_rmbUser"/>记住用户名和密码<br />
<input type="submit" id="sub" value="登录" onclick="Save()"/>
</div>
<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 expires:(Number|Date)有效期;设置一个整数时,单位是天;也可以设置一个日期对象作为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>
</body>