fname,production,prod From不显示Cookie值。每次都返回“null null null”。但是最后一个条目的日期正常显示。 有必要创建和处理cookie,存储任何表单字段(fname,production,prodForm)的值,这是在最后一次填充尝试和日期时引入的。
功能setCookie中的问题是什么? (因为我得到了上次访问的日期)请帮助修复它。
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Medicines</title>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="formaFunctions.js"></script>
<script type="text/javascript" src="cookies.js"></script>
</head>
<body onload="checkCookie()">
<h2>Medicines</h2>
<div id="error"></div>
<br>
<form name="myForm"
action="http://10.12.53.159:8111/stud"
onsubmit="return validateForm()"
method="post">
<table border="2">
<tr>
<td>Name:</td>
<td><input type="text" name="fname"></td>
</tr>
<tr><td>Production:</td>
<td><input type="text" name="production"></td>
</tr>
<tr><td>Production form:</td>
<td><select name="prodForm">
<option>Powder</option>
<option>Tablets</option>
<option>Dragee</option>
<option>Mixture</option>
<option>Salve</option>
</select></td>
</tr>
</table>
<p><input type="submit" value="Find" /></p>
</form>
<div id="cookie"></div>
</body>
</html>
</code>
使用Javascript:
function setCookie(name,value,exdays){
var cookie_string = name+"="+escape(value);
if(exdays){
var exdate = new Date();
exdate.setTime(exdate.getTime()+(exdays*24*60*60*1000));
var expires = "; expires="+exdate.toGMTString();
}
document.cookie = cookie_string;
}
function getCookie(c_name){
var i,x,y, ARRcookies=document.cookie.split(';');
for(i=0; i<ARRcookies.length; i++){
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x = x.replace(/^\s+|\s+$/g,"");
if (x==c_name){
return unescape(y);
}
}
}
function checkCookie(){
var string;
var fname = getCookie("fname");
var production = getCookie("production");
var prodForm = getCookie("prodForm");
if(fname!=null&&fname!=""){
string = fname;
}
else{
setCookie("fname", fname, 30);
}
if(production!=null&&production!=""){
string += production;
}
else{
setCookie("production", production, 30);
}
if(prodForm!=null&&prodForm!=""){
string += prodForm;
}
else{
setCookie("prodForm", prodForm, 30);
}
string += getCookie("lastVisit");
document.getElementById('cookie').innerHTML = string;
setCookie("lastVisit",new Date().toLocaleString(),30);
}