我写的js,不知怎么没有效果...function addCookie(){
var str1 = "babygoods=" + document.getElementById("babygoods").value;
var str2 = "goods_cat=" + document.getElementById('goods_cat').value;
document.cookie = str1;
document.cookie = str2;
alert(document.cookie);
}function getCookie(){//获取指定名称的cookie的值
var arrStr = document.cookie.split(";");
var babygoods = document.getElementById("babygoods");
for(var i = 0;i < arrStr.length;i++){
var temp = arrStr[i].split("=");
alert(temp[0]+"="+temp[1]);
if(temp[0] == "babygoods"){
option = document.createElement("option");
option.appendChild(document.createTextNode(temp[1]));
option.value = temp[1];
option.selected = selected;
babygoods.appendChild(option);
}
if(temp[0] == "goods_cat"){
document.getElementById("goods_cat") = temp[1];
//alert(temp[1]);
}
}
}window.onload = function () {
getCookie();
}当执行查询的时候,执行addCookie;
然后装载页面的时候,读取出来。
读取的时候,cookie的值能取到,但就是弄不进去。。
var str1 = "babygoods=" + document.getElementById("babygoods").value;
var str2 = "goods_cat=" + document.getElementById('goods_cat').value;
document.cookie = str1;
document.cookie = str2;
alert(document.cookie);
}function getCookie(){//获取指定名称的cookie的值
var arrStr = document.cookie.split(";");
var babygoods = document.getElementById("babygoods");
for(var i = 0;i < arrStr.length;i++){
var temp = arrStr[i].split("=");
alert(temp[0]+"="+temp[1]);
if(temp[0] == "babygoods"){
option = document.createElement("option");
option.appendChild(document.createTextNode(temp[1]));
option.value = temp[1];
option.selected = selected;
babygoods.appendChild(option);
}
if(temp[0] == "goods_cat"){
document.getElementById("goods_cat") = temp[1];
//alert(temp[1]);
}
}
}window.onload = function () {
getCookie();
}当执行查询的时候,执行addCookie;
然后装载页面的时候,读取出来。
读取的时候,cookie的值能取到,但就是弄不进去。。
cookie的写入最好指定过期时间,对AddCookie这个方法最好这样改下://set Cookie value
function AddCookie(name, value)
{
var expdate = new Date();
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))
+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
+((secure == true) ? "; secure" : "");
}
option.value = temp[1];
option.selected = selected;
babygoods.appendChild(option);
如上红色的地方,selected我没有找到定义。
option.selected =true;
对babygoods我的理解是它是一个select对象。
babygoods.appendChild(option);
对子元素的添加应该是这种形式:
babygoods.options.add(option);
但是options[0].value或是option[1].value后面这些内容都为空,这是为什么...
function addCookie(){
var str1 = "bg_options=" + escape(document.getElementById("babygoods").options);
var str2 = "gc_options=" + escape(document.getElementById("goods_cat").options);
document.cookie = str1;
document.cookie = str2;
}//重点在这儿
function getCookie(){//获取指定名称的cookie的值
var babygoods = document.getElementById("babygoods");
var arrStr = document.cookie.split(";");
alert(arrStr);
for(var i = 0;i < arrStr.length;i++){
var temp = arrStr[i].split("=");
var str = temp[0].trim();
if(str == "bg_options"){
var options = unescape(temp[1]);
//这个alert出来了,长度为8,说明存在cookie中了.
alert(options.length);
//但是让人极其不爽的是这里什么也没有...无论是options[0].value
alert(options[1].value);
for(var j = 0;j<options.length;j++){
alert(options[j].value);
// babygoods.options.appendChild(options[j]);
}
/*
option = document.createElement("option");
option.appendChild(document.createTextNode(temp[1].trim()));
option.value = temp[1];
option.selected = true;
babygoods.options.appendChild(option);*/
}//这里还没有处理
if(temp[0] == "gc_options"){
document.getElementById("goods_cat") = temp[1];
//alert(temp[1]);
}
}
}好了,希望大家帮忙看看,分不够我再加..