用cookie保存选择的选项

时间:2015-01-30 17:19    点击:

每次刷新不会被重置啦


<select name="sex" id="sex" onchange="save()">
    <option  value="01" selected >男</opton>
    <option  value="02" >女</opton>
</select>

<input id="s1" type="radio" name="xueli" value="0" onclick="save()"/>本科
<input id="s2" type="radio" name="xueli" value="1" checked onclick="save()"/>专科

<script language="javascript" type="text/javascript">
    function save() {
        selectIndex = document.getElementById("sex").selectedIndex;
        document.cookie = 'selectIndex =' + selectIndex;
        radios = document.getElementsByName("xueli");
        for (i = 0; i < radios.length; i++) {
            if (radios[i].checked) document.cookie = 'radioindex =' + i;
        }
    }
    window.onload = function () {
        var cooki = document.cookie;
        if (cooki != "") {
            cooki = "{\"" + cooki + "\"}";
            cooki = cooki.replace(/\s*/g, "").replace(/=/g, '":"').replace(/;/g, '","');
            var json = eval("(" + cooki + ")"); //将coolies转成json对象
            document.getElementById("sex").options[json.selectIndex].selected = true;
            document.getElementsByName("xueli")[json.radioindex].checked = true;
        }
        else
            save();
    }
</script>

来源:未知//所属分类:Web开发/更新时间:2015-01-30 17:19
顶一下
(3)
50%
踩一下
(3)
50%
上一篇:没有了
下一篇:没有了
相关内容