document.cookie = "userId=678";
        document.cookie = "userName=2009";
        var strCookie = document.cookie;
        var arrCookie = strCookie.split(";");
        var userId;
        var userName;        //遍历cookie数组,处理每个cookie对 
        for (var i = 0; i < arrCookie.length; i++) {
            var arr = arrCookie[i].split("=");
            //找到名称为userId的cookie,并返回它的值 
            if (arr[0] == "userId"&&arr[2] == "userName") {
                
                document.getElementById("TextBox1").value = arr[1];
                document.getElementById("TextBox2").value = arr[3];            }
        }请问各位大神,这段代码错在哪里?我应该怎么改,我要的效果是把userId的值显示在TextBox1里面,userName的值显示在TextBox2里面。

解决方案 »

  1.   

    arrCookie= arrCookie[i].split("=")后数组就是arrCookie[0]="userId=678"
    你 arr = arrCookie[i].split("=")后arr[0]是userId,arr[1]是678
    改成如下试试:
    var arr;
    for (var i = 0; i < arrCookie.length; i++)
    { arr = arrCookie[i].split("=");
      switch(arr[0])
      { case "userId":
             document.getElementById("TextBox1").value = arr[1];
             break;
         case "userName":
             document.getElementById("TextBox2").value = arr[1];
             break;
       }
    }