日期:2014-05-16  浏览次数:20420 次

js保存用户名
JSP表单页面
<form action="login" method="post"> 
                用户名:<input type="text" name="username" id="username" /><br> 
                密码:<input type="password" name="password" /><br /> 
                <input type="checkbox" id="remember" checked="checked"> 
                记住用户名<input type="submit" value="登录" id="sub" /> 
            </form> 

JS操作cookie
<script type="text/javascript"> 
    $(document).ready(function() { 
            var name = getCookie("username"); 
            //$("#username")[0].value=name;  
            $("#username").val(name); 
 
            /*
            * 当点击登录按钮时 判断是否勾选记住用户名 
            * 如果勾选  则将用户名保存在Cookie中 否则删除 
            */ 
            $("#sub").click(function() { 
                if ($("#remember").attr("checked") == true) { 
                    //获得用户名  
                    var username = $("#username").val(); 
                    //设置cookie 
                    setCookie("username", username); 
                } else { 
                    //删除cookie 
                    delCookie("username")  
                } 
            }) 
            function setCookie(name, value) { 
                var Days = 60; //cookie 将被保存两个月  
                var exp = new Date(); //获得当前时间  
                exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000); //换成毫秒 
                document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString(); 
            } 
    &nb