select刷新后,保持选定状态,Cookies存储select选定状态信息

    //cookies存储select选定值,防止刷新后没了
    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("pselect").options[json.selIndex].selected = true;//保持原来的选项 
    }else 
    save(); 
    } 
    
    function selectP(){
        var options=$("#pselect option:selected");
        var selectP=options.val();
        selectIndex = document.getElementById("pselect").selectedIndex; 
        document.cookie = 'selIndex =' + selectIndex;//保存在cookie中,键名为selIndex 

        //alert(p);
        $.ajax(
                {
                   type:"post",  
                   url:"<%=basePath%>servlet/UserServlet",
                   data:"oper=6&selectP=" + selectP,
                   success:function(data) {
                       //alert(data);
                       
                         location.reload();

                   }
                });
    }

 

posted @ 2015-04-30 14:36  阿尔法钻石狗  阅读(927)  评论(0编辑  收藏  举报