刷新页面时 select、raido值保持不变

刷新页面时,要使下拉菜单(select)、raido保持不变,用ajax是无法实现的。我想只能通过cookies才能实现。刷新前先把select或radio的值保存在cookies中,刷新后再填回去。下面是测试代码:

<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>


 


 

你可能感兴趣的:(JavaScript,json,Ajax,function,Cookies,input)