我写的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的值能取到,但就是弄不进去。。

解决方案 »

  1.   

    看了一下,发觉有几个问题:
    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);
      

  2.   

    getcookie 中 遍历 select 的 option value 值,检查是哪个 index, 再设置 select 的 selectedIndex
      

  3.   

    动态添加的内容是存在内存中!在真正的HTML结构中是不存在!当然会在刷新时丢失了!除非用XML作缓存
      

  4.   

    question1:用xml作为缓存怎么弄啊?question2:再者,我现用js cookie,我把select的options存在cookie中,在读取的时候,虽然options的长度是大于0的,
    但是options[0].value或是option[1].value后面这些内容都为空,这是为什么...
      

  5.   

    补充一下代码//查询时添加的cookie,虽然没有添加时间,因为我想cookie存在的期限为浏览器只要不关闭就行.
    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]);
         }
       } 
    }好了,希望大家帮忙看看,分不够我再加..