总结要点:
一、封装的一个cookie函数;
二、获取input的value值:$("input[name=user]").val()
三、设置input的value值:$("input[name=user]").attr("value",getCookie("user"));
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="jquery.min.js"></script>
<script>
function setCookie(name,value,iDay){
var oDate=new Date();
oDate.setDate(oDate.getDate()+iDay);
document.cookie=name+'='+value+';expires='+oDate;
}
function getCookie(name){
var arr=document.cookie.split(";");
for(var i=0;i<arr.length;i++){
var arr2=arr[i].split("=");
if(arr2[0]==name){
return arr2[1];
}
return '';
}
}
function removeCookie(name){
setCookie(name,"1",-1);
}
$(function(){
$("#form1").submit(function(){
setCookie("user",$("input[name=user]").val(),2);
});
$("input[name=user]").attr("value",getCookie("user"));
$("a").click(function(){
removeCookie("user");
$("input[name=user]").attr("value","");
});
});
</script>
</head>
<body>
<form id="form1" action="#">
用户名:<input type="text" name="user">
密码:<input type="password" name="pass">
<input type="submit" value="提交">
<a href="javascript:;">清除记录</a>
</form>
</body>
</html>