日期:2014-05-18  浏览次数:20438 次

js刷新后记忆之前的值
问题是这样的:
有个文本框之前是有默认值,但是如果用户一输入值那么刷新后文本框里的值还是用户输入的值。
麻烦各位帮忙用JS写一下。或提供一下思路也行。谢谢各位大侠了。

------解决方案--------------------
用cookie吧
JScript code


function SetCookie(name,value) {
    var key='';
    var Days = 2;
    var exp = new Date();
    var domain = "";
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    if (key == null || key == "") {
        document.cookie = name + "=" + encodeURI(value) + ";expires=" + exp.toGMTString() + ";path=/;domain=" + domain + ";";
    }
    else {
        var nameValue = GetCookie(name);
        if (nameValue == "") {
            document.cookie = name + "=" + key + "=" + encodeURI(value) + ";expires=" + exp.toGMTString() + ";path=/;domain=" + domain + ";";
        }
        else {
            var keyValue = getCookie(name, key);
            if (keyValue != "") {
                nameValue = nameValue.replace(key + "=" + keyValue, key + "=" + encodeURI(value));
                document.cookie = name + "=" + nameValue + ";expires=" + exp.toGMTString() + ";path=/;domain=" + domain + ";";
            }
            else {
                document.cookie = name + "=" + nameValue + "&" + key + "=" + encodeURI(value) + ";expires=" + exp.toGMTString() + ";path=/;" + domain + ";";
            }
        }
    }
}

function GetCookie(name) {
    var nameValue = "";
    var key="";
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg)) {
        nameValue = decodeURI(arr[2]);
    }
    if (key != null && key != "") {
        reg = new RegExp("(^| |&)" + key + "=([^(;|&|=)]*)(&|$)");
        if (arr = nameValue.match(reg)) {
            return decodeURI(arr[2]);
        }
        else return "";
    }
    else {
        return nameValue;
    }
}

------解决方案--------------------
JScript code
//保存用户条件
var key = "<%=key %>";
var stardate = "<%=stardate %>";
var dataend = "<%=dataend %>";
if (document.getElementById("serchinput").value == "") {
    document.getElementById("serchinput").value = key;
}
if (document.getElementById("datastart").value == "") {
    document.getElementById("datastart").value = stardate;
}
if (document.getElementById("dataend").value == "") {
    document.getElementById("dataend").value = dataend;
}
        
</script>