用JS保存Cookie 用户名 密码实现问题 本帖最后由 showbo 于 2014-07-12 16:50:21 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 form没有load事件,放到body标签里面而且cookie是键值对形式存储,而不是存储一串字符,document没有addCookie方法<html> <head> <Script type="text/javascript"> function save(){ if(confirm("是否保存用户名密码?")){ saveUserName(); saveUserPass(); return true ; }else{ return false; } } function saveUserName(){ var userName =''; if(document.myForm.uname.value != null) userName = document.myForm.uname.value;document.cookie="un="+userName // document.addCookie(userName+"and"); } function saveUserPass(){ var userPass =''; if(document.myForm.upass.value != null) userPass = document.myForm.upass.value;document.cookie="pwd="+userPass // document.addCookie(userPass); }function getCookie(key,cookie){var r=new RegExp(key+'=([^;]+)','i')var m=r.exec(cookie);if(m)return m[1];return ''} function show(){ var strCookie = document.cookie; var userName ; var userPass ; if(strCookie == null){ document.myForm.uname.value=""; document.myForm.upass.value=""; }else{ userName = getCookie('un',strCookie); userPass = getCookie('pwd',strCookie); document.myForm.uname.value = userName; document.myForm.upass.value = userPass; } } </Script> </head> <body onload="show()"> <form action="Cookie.jsp" name="myForm" method="post" > 用户名:<input type="text" name="uname" /> 密  码:<input type="text" name="upass" /> <input type="submit" name="submit" value="提交" onclick="return save()"/> <input type="reset" name="reset" value="重置"/> </form> <body></html> 谢谢 你的解答,我之前 是想用document。cookie 获取整个cookie 字符串 不可以嘛,谢谢 你的帮忙 xml文件中的<![CDATA[]]>中包含<![CDATA[]]>怎么办? 小需求-求高效解决方案 jquery如何获取ID 怎样使页面格式不受浏览器的兼容模式影响 全国省份、城市、城镇(辖区)方便调用的JS控件 怎么判断英文和中文逗号是一种逗号? 100分求超连接变色的问题解决再加100分 谁有javascript的对象结构图呀,我想下载下来打印一下 easyuidatagrid onClickCell 函数中做页面跳转问题 圣杯布局的原理什么 怎么提交按钮获取选中的src 下载了一个判断浏览器类型的代码,不知道怎么用,求教
<html>
<head>
<Script type="text/javascript">
function save(){
if(confirm("是否保存用户名密码?")){
saveUserName();
saveUserPass();
return true ;
}else{
return false;
}
}
function saveUserName(){
var userName ='';
if(document.myForm.uname.value != null)
userName = document.myForm.uname.value;
document.cookie="un="+userName
// document.addCookie(userName+"and");
}
function saveUserPass(){
var userPass ='';
if(document.myForm.upass.value != null)
userPass = document.myForm.upass.value;
document.cookie="pwd="+userPass
// document.addCookie(userPass);
}
function getCookie(key,cookie){
var r=new RegExp(key+'=([^;]+)','i')
var m=r.exec(cookie);
if(m)return m[1];return ''
}
function show(){
var strCookie = document.cookie;
var userName ;
var userPass ;
if(strCookie == null){
document.myForm.uname.value="";
document.myForm.upass.value="";
}else{ userName = getCookie('un',strCookie);
userPass = getCookie('pwd',strCookie);
document.myForm.uname.value = userName;
document.myForm.upass.value = userPass;
}
}
</Script>
</head>
<body onload="show()">
<form action="Cookie.jsp" name="myForm" method="post" >
用户名:<input type="text" name="uname" />
密  码:<input type="text" name="upass" />
<input type="submit" name="submit" value="提交" onclick="return save()"/>
<input type="reset" name="reset" value="重置"/>
</form>
<body>
</html>
谢谢 你的解答,我之前 是想用document。cookie 获取整个cookie 字符串 不可以嘛,谢谢 你的帮忙