javascript注册不了cookie的问题 你都没调用checkCookie。。还有本地file调试不要用chrome,本地cookie chrome不存储 checkCookie() 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 搭建一个简单的Web服务器,把html扔里面就可以在浏览器里设置cookies了 我在body里调用了,body是这样写的:<body onLoad="checkCookie()"> 求解决呀~我用360、chrome、ie都试过了,都不能保存cookie,这是为神马呀? 360极速模式下就是chrome,ie什么版本的,chrome下没发布的页面不会保存cookie发你代码全来,问个问题东一点西一点,哪知道你哪里出问题 好嘛,我全部的代码是这样的:<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>表单验证</title><script type="text/javascript">var audioElement = document.createElement('audio'); audioElement.setAttribute('src', 'song.mp3');/*function clear(){ Source=document.body.innerHTML; document.open(); document.write("你想看源代码吗?问我要呀~哇咔咔~"); document.close(); document.body.innerHTML=Source;} */function PlayAudio() //播放音频文件{ audioElement.load; audioElement.play(); }function checkForm() //检查内容是否为空{ if(document.test.username.value == "") { // alert("用户名不能为空!"); // document.test.username.focus(); document.getElementById("usernameerr").innerHTML="<font color='red' size='-2'>请输入用户名!" //插入红色的提醒字 return false; } PlayAudio(); document.getElementById("usernameerr").innerHTML="<font color='green' size='-2'>输入正确!" // document.getElementById("usernameerr").innerHTML="<img src='1.png' height='20' width='20'>" return true;}function CheckPSW() //检查两个元素值是否相等{ if(document.test.password1.value != document.test.password2.value) { alert("两次输入的密码不一致!"); document.text.password2.focus(); return false; } return true;}function onlyNum(event) //输入只能为数字{ //var event = e || window.event; if(!((event.keyCode>=48&&event.keyCode<=57))||(event.keyCode>=96&&event.keyCode<=105)) { window.event.keyCode = 0 ; alert("请输入数字!"); document.test.age.value=""; document.test.age.focus(); return false; } return true;}function onlyEng(e) //输入只能为英文{ if(!(e.keyCode >= 65 && e.keyCode<=90)) { alert("请输入英文!"); document.test.EngName.focus(); return false; } return true;}function checkLength() //检查输入内容的长度{ if(document.test.phoneNum.value.length != 11) { alert("手机号码只能是11位!"); return false; } //alert("正确的输入!"); return true; alert("正确的输入!");}function show() { myForm = document.test.elements[0].form; //以上使用对象的form属性引用文档的表单 newWin = open("","",height=400,width=400); //另外打开一个窗口 newWin.document.write("<h3>表单元素信息:</h3>"); for(i=0;i<myForm.length;i++) //循环检测表单元素类型 { if(myForm.elements[i].type=="text") myType = "文本框"; else if(myForm.elements[i].type=="password") myType="密码框"; else if(myForm.elements[i].type=="textarea") myType="文本域"; else if(myForm.elements[i].type=="file") myType="文件选择框"; else if(myForm.elements[i].type=="button") myType="按钮"; else myType="其他元素"; newWin.document.write(myType+"中的值为:"+myForm.elements[i].value+"<p>") }}function getCookie(c_name) //检查是否已设置cookie{ if(document.cookie.length>0) { c_start = document.cookie.indexOf(c_name+"=") //indexOf()方法可返回某个指定的字符串值在字符串中首次出现的位置。 //参考:http://blog.163.com/very_apple/blog/static/2775923620109273237597/ if (c_start != -1) { c_start=c_start + c_name.length + 1 c_end=document.cookie.indexOf(";",c_start) if(c_end == -1) { c_end = document.cookie.length } return unescape(document.cookie.substring(c_start,c_end))//unescape 方法返回一个包含 charstring 内容的字符串值 //参考:http://chali22.blog.163.com/blog/static/4000369200791525738395/ } } return ""}function setCookie(c_name,value,expiredays) //创建一个cookie{ exdate=new Date() exdate.setDate(exdate.getDate()+expiredays) document.cookie=c_name+"="+escape(value)+((expiredays==null)?";":";expires="+exdate.toGMTString()) // var cookieString=c_name+"="+escape(value); //判断是否设置过期时间 /* if(expireHours>0){ var date=new Date(); date.setTime(date.getTime+expireHours*3600*1000); cookieString=cookieString+"; expire="+date.toGMTString(); } document.cookie=cookieString; */}function checkCookie(){ username=getCookie('username') if(username != null && username != "") { alert('欢迎再次回来'+username+'!') } else { username=prompt('请输入你的名字:',"") if(username != null && username != "") { setCookie('username',username,365) } }}</script></head><body onLoad="checkCookie()"><form name="test" onsubmit="show()">用户名:<input type="text" name="username" onblur="checkForm()" ><span id="usernameerr"></span><!--<input type="button" onclick="checkForm()" name="check" value="检查">--></br>密码:<input type="password" name="password1" onfocus=this.select() ></br>确认密码:<input type="password" name="password2"><input type="button" onclick="CheckPSW()" name="check" value="检查"></br>输入年龄:<input type= "text" onkeypress="onlyNum(event)" name="age" onfocus=this.select()><br/>输入手机号码:<input type="text" onkeypress="onlyNum(event)" name="phoneNum" onfocus=this.select()><input type="button" onclick="checkLength()" value="检查"><br/>个人简介:<textarea rows=4 onfocus=this.select() onfocus=this.select()></textarea><br/>英文名字:<input type="text" onkeypress="onlyEng(event2)" name="EngName" onfocus=this.select()><br/>选择文件:<input type=file name=file1 onfocus=this.select()><br/><input type=submit value="提交"><input type=reset value="重置"><br/></body></html> IE8-,firefox测试没有问题,没发布的网站已经告诉过你不要使用chrome,就是webkit核心的浏览器来查看,不会保存cookie 一个奇奇怪怪的js问题 easyui的tree的根节点显示方式有办法横向排列显示吗 textarea 插入图片后的问题~~~~ 急,请高手赐教! 怎么样根据select中的值变换网页中的元素? onpropertychange事件问题 弹出一个DIV,里面有个播放器,IE10下面一切正常, IE8下面播放器大小自动变大了 这段javascript代码怎么不能输出阿? 假如有一个javascript的常用工具类,你希望它包含什么功能? vue+element ui 怎么在select下拉框里面嵌入树形组件啊 怎样直接post表单里的内容到一个网址去? js中调用div层,急求大神回复!!!
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>表单验证</title>
<script type="text/javascript">
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'song.mp3');
/*
function clear()
{
Source=document.body.innerHTML;
document.open();
document.write("你想看源代码吗?问我要呀~哇咔咔~");
document.close();
document.body.innerHTML=Source;
}
*/
function PlayAudio() //播放音频文件
{
audioElement.load;
audioElement.play();
}
function checkForm() //检查内容是否为空
{
if(document.test.username.value == "")
{
// alert("用户名不能为空!");
// document.test.username.focus();
document.getElementById("usernameerr").innerHTML="<font color='red' size='-2'>请输入用户名!" //插入红色的提醒字
return false;
}
PlayAudio();
document.getElementById("usernameerr").innerHTML="<font color='green' size='-2'>输入正确!"
// document.getElementById("usernameerr").innerHTML="<img src='1.png' height='20' width='20'>"
return true;
}
function CheckPSW() //检查两个元素值是否相等
{
if(document.test.password1.value != document.test.password2.value)
{
alert("两次输入的密码不一致!");
document.text.password2.focus();
return false;
}
return true;
}
function onlyNum(event) //输入只能为数字
{
//var event = e || window.event;
if(!((event.keyCode>=48&&event.keyCode<=57))||(event.keyCode>=96&&event.keyCode<=105))
{
window.event.keyCode = 0 ;
alert("请输入数字!");
document.test.age.value="";
document.test.age.focus();
return false;
}
return true;
}
function onlyEng(e) //输入只能为英文
{
if(!(e.keyCode >= 65 && e.keyCode<=90))
{
alert("请输入英文!");
document.test.EngName.focus();
return false;
}
return true;
}
function checkLength() //检查输入内容的长度
{
if(document.test.phoneNum.value.length != 11)
{
alert("手机号码只能是11位!");
return false;
}
//alert("正确的输入!");
return true;
alert("正确的输入!");
}
function show()
{
myForm = document.test.elements[0].form;
//以上使用对象的form属性引用文档的表单
newWin = open("","",height=400,width=400); //另外打开一个窗口
newWin.document.write("<h3>表单元素信息:</h3>");
for(i=0;i<myForm.length;i++) //循环检测表单元素类型
{
if(myForm.elements[i].type=="text")
myType = "文本框";
else if(myForm.elements[i].type=="password")
myType="密码框";
else if(myForm.elements[i].type=="textarea")
myType="文本域";
else if(myForm.elements[i].type=="file")
myType="文件选择框";
else if(myForm.elements[i].type=="button")
myType="按钮";
else
myType="其他元素";
newWin.document.write(myType+"中的值为:"+myForm.elements[i].value+"<p>")
}
}function getCookie(c_name) //检查是否已设置cookie
{
if(document.cookie.length>0)
{
c_start = document.cookie.indexOf(c_name+"=") //indexOf()方法可返回某个指定的字符串值在字符串中首次出现的位置。
//参考:http://blog.163.com/very_apple/blog/static/2775923620109273237597/
if (c_start != -1)
{
c_start=c_start + c_name.length + 1
c_end=document.cookie.indexOf(";",c_start)
if(c_end == -1)
{
c_end = document.cookie.length
}
return unescape(document.cookie.substring(c_start,c_end))//unescape 方法返回一个包含 charstring 内容的字符串值
//参考:http://chali22.blog.163.com/blog/static/4000369200791525738395/
}
}
return ""
}
function setCookie(c_name,value,expiredays) //创建一个cookie
{
exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+"="+escape(value)+((expiredays==null)?";":";expires="+exdate.toGMTString())
// var cookieString=c_name+"="+escape(value);
//判断是否设置过期时间
/* if(expireHours>0){
var date=new Date();
date.setTime(date.getTime+expireHours*3600*1000);
cookieString=cookieString+"; expire="+date.toGMTString();
}
document.cookie=cookieString;
*/
}function checkCookie()
{
username=getCookie('username')
if(username != null && username != "")
{
alert('欢迎再次回来'+username+'!')
}
else
{
username=prompt('请输入你的名字:',"")
if(username != null && username != "")
{
setCookie('username',username,365)
}
}
}</script>
</head>
<body onLoad="checkCookie()">
<form name="test" onsubmit="show()">
用户名:<input type="text" name="username" onblur="checkForm()" >
<span id="usernameerr"></span>
<!--<input type="button" onclick="checkForm()" name="check" value="检查">-->
</br>
密码:<input type="password" name="password1" onfocus=this.select() ></br>
确认密码:<input type="password" name="password2">
<input type="button" onclick="CheckPSW()" name="check" value="检查">
</br>
输入年龄:<input type= "text" onkeypress="onlyNum(event)" name="age" onfocus=this.select()>
<br/>
输入手机号码:<input type="text" onkeypress="onlyNum(event)" name="phoneNum" onfocus=this.select()>
<input type="button" onclick="checkLength()" value="检查"><br/>
个人简介:<textarea rows=4 onfocus=this.select() onfocus=this.select()></textarea>
<br/>
英文名字:<input type="text" onkeypress="onlyEng(event2)" name="EngName" onfocus=this.select()>
<br/>
选择文件:<input type=file name=file1 onfocus=this.select()><br/>
<input type=submit value="提交">
<input type=reset value="重置">
<br/>
</body>
</html>