Javascript表单cookie -选择只打开前10个索引的cookie



我以前从未使用过cookie,所以我使用了一段我非常不熟悉的代码。

它工作得很好,直到我刚刚注意到,对于选择框,它不能为第10个索引之后的任何值工作。(适用于索引10或以上)。

我已经查看了存储在我系统上的cookie,它似乎没有正确地保存它们。(我看到select10) ETC存储正确

但是,当它运行onload of body时,它没有正确加载值。

下面是我使用的cookie代码:

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var expDays = 100;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function getCookieVal (offset) {  
    var endstr = document.cookie.indexOf (";", offset);  
    if (endstr == -1) { endstr = document.cookie.length; }
    return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {  
    var arg = name + "=";  
    var alen = arg.length;  
    var clen = document.cookie.length;  
    var i = 0;  
    while (i < clen) {    
        var j = i + alen;    
        if (document.cookie.substring(i, j) == arg) return getCookieVal (j);    
        i = document.cookie.indexOf(" ", i) + 1;    
        if (i == 0) break;   
    }  
    return null;
}

function SetCookie (name, value) {  
    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;  
    document.cookie = name + "=" + escape (value) + 
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
    ((path == null) ? "" : ("; path=" + path)) +  
    ((domain == null) ? "" : ("; domain=" + domain)) +    
    ((secure == true) ? "; secure" : "");
}
// use the following code to call it:
//  <body onLoad="cookieForms('open', 'form_1', 'form_2', 'form_n')" onUnLoad="cookieForms('save', 'form_1', 'form_2', 'form_n')">
function cookieForms() {  
    var mode = cookieForms.arguments[0];
    for(f=1; f<cookieForms.arguments.length; f++) {
        formName = cookieForms.arguments[f];
        if(mode == 'open') {    
            cookieValue = GetCookie('saved_'+formName);
            if(cookieValue != null) {
                var cookieArray = cookieValue.split('#cf#');
                if(cookieArray.length == document[formName].elements.length) {
                    for(i=0; i<document[formName].elements.length; i++) {
                        if(cookieArray[i].substring(0,6) == 'select') { document[formName].elements[i].options.selectedIndex = cookieArray[i].substring(7, cookieArray[i].length-1); }
                        else if((cookieArray[i] == 'cbtrue') || (cookieArray[i] == 'rbtrue')) { document[formName].elements[i].checked = true; }
                        else if((cookieArray[i] == 'cbfalse') || (cookieArray[i] == 'rbfalse')) { document[formName].elements[i].checked = false; }
                        else { document[formName].elements[i].value = (cookieArray[i]) ? cookieArray[i] : ''; }
                    }
                }
            }
        }
        if(mode == 'save') {    
            cookieValue = '';
            for(i=0; i<document[formName].elements.length; i++) {
                fieldType = document[formName].elements[i].type;
                if(fieldType == 'password') { passValue = ''; }
                else if(fieldType == 'checkbox') { passValue = 'cb'+document[formName].elements[i].checked; }
                else if(fieldType == 'radio') { passValue = 'rb'+document[formName].elements[i].checked; }
                else if(fieldType == 'select-one') { passValue = 'select'+document[formName].elements[i].options.selectedIndex; }
                else { passValue = document[formName].elements[i].value; }
                cookieValue = cookieValue + passValue + '#cf#';
            }
            cookieValue = cookieValue.substring(0, cookieValue.length-4); // Remove last delimiter
            SetCookie('saved_'+formName, cookieValue, exp);     
        }   
    }
}
//  End -->
</script>

我认为问题出在下面一行,大约在上面代码块的3/4处(第68行):

if(cookieArray[i].substring(0,6) == 'select') { document[formName].elements[i].options.selectedIndex = cookieArray[i].substring(7, cookieArray[i].length-1); }

仅供参考,以下是我使用的开始正文标签:

<body style="text-align:center;" onload="cookieForms('open', 'ramsform', 'decksform', 'hullsform', 'crewform', 'shipwrightform'); Swap(crew0z,'crew0i'); Swap(crew1z,'crew1i'); Swap(crew2z,'crew2i'); Swap(crew3z,'crew3i'); Swap(crew4z,'crew4i'); Swap(crew5z,'crew5i'); Swap(crew6z,'crew6i'); Swap(crew7z,'crew7i'); Swap(crew8z,'crew8i'); Swap(crew9z,'crew9i');" onunload="cookieForms('save', 'ramsform', 'decksform', 'hullsform', 'crewform', 'shipwrightform');">

(请忽略swap(),因为它们是不相关的)

我正在工作的页面可以找到:http://webhostlet.com/POP.htm

opensave代码中,更改:

document[formName].elements[i].options.selectedIndex

:

document[formName].elements[i].selectedIndex

options是所有选项的数组,selectedIndex属性属于包含它们的select元素。

改变:

cookieArray[i].substring(7, cookieArray[i].length-1)

:

cookieArray[i].substring(6)

你错了1,因为你忘了这是基于0的计数。第二个参数是不需要的,它默认为字符串的其余部分。

对于前10个菜单项起作用的原因是substring的一个特点:如果第二个参数低于第一个参数,它会交换它们!因此,"select5".substring(7, 6)被视为"select5".substring(6, 7),后者获取字符串的最后一个字符。但是对于较长的字符串,它是`"select35".substring(7, 7),这是一个空字符串。

最新更新