将选择器保存在cookie中并获取该cookie

时间:2015-01-06 13:21:03

标签: javascript jquery select cookies

我想在我的webapp中向用户询问他们想要打开哪个学校网站的下拉菜单,然后将其保存在cookie中并在下次打开webapp时自动打开该网站。我不擅长JS所以请解释一下。

提前感谢。

<head>
<script>
 <!-- Cookie script -->
</script>
</head>
<body>
    <form>
        <select id="class">
          <option value="Choose">Choose</option>
          <option value="flah">School#1</option> <!-- Should redirect to site #1 -->
          <option value="june">School#2</option> <!-- Should redirect to site #2 -->
        </select>
        <button type="submit">V&auml;lj</button>
    </form>
</body>

更新

    &LT;

form>
        <select id="class">
          <option value="Choose">Choose</option>
          <option value="flah">School#1</option> <!-- Should redirect to site #1 -->
          <option value="june">School#2</option> <!-- Should redirect to site #2 -->
        </select>
        <button type="submit" onclick="setCookie()">V&auml;lj</button>
    </form>

<script type="text/javascript" language="javascript">    
    function setCookie(cookiename, cookievalue, cookieexdays) {
        var d = new Date();
        d.setTime(d.getTime() + (cookieexdays*24*60*60*1000));
        var expires = "expires="+d.toUTCString();
        document.cookie = cookiename+ "=" + cookievalue+ "; " + expires;
    }

    function getCookie(cookiename) {
        var name = cookiename+ "=";
        var ca = document.cookie.split(';');
        for(var i=0; i<ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1);
            if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
        }
        return "";
    }

    function checkCookie() {
        var school = getCookie("SelectedSchool");
        if (school!= "") {
            //redirect to user to link
            alert("Welcome to the " + school);
        } else {
            user = prompt("Please choose your name:", "");
            if (school != "" && school != null) {
                setCookie("SelectedSchool", school, 365);
            }
        }
    }
</script>

2 个答案:

答案 0 :(得分:1)

要创建cookie,请使用:

document.cookie="key=value";

获取select的值(jquery中的示例):

var val = $("#class").val();

将其保存在Cookie中。 现在用户下次登录时使用以下方式读取cookie:

var x = document.cookie;

并将其重定向:

location.href = "link";
祝你好运

答案 1 :(得分:0)

试试这个,这些是你可以用来保存,获取或检查cookie的JavaScript函数

<head>
<script>
 <!-- Cookie script -->
</script>
</head>
<body>
    <form>
        <select id="class">
          <option value="Choose">Choose</option>
          <option value="flah">School#1</option> <!-- Should redirect to site #1 -->
          <option value="june">School#2</option> <!-- Should redirect to site #2 -->
        </select>
        <button type="submit">V&auml;lj</button>
    </form>

    <script type="text/javascript" language="javascript">
        function setCookie(cookiename, cookievalue, cookieexdays) {
            var d = new Date();
            d.setTime(d.getTime() + (cookieexdays*24*60*60*1000));
            var expires = "expires="+d.toUTCString();
            document.cookie = cookiename+ "=" + cookievalue+ "; " + expires;
        }

        function getCookie(cookiename) {
            var name = cookiename+ "=";
            var ca = document.cookie.split(';');
            for(var i=0; i<ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1);
                if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
            }
            return "";
        }

        function checkCookie() {
            var school = getCookie("SelectedSchool");
            if (school!= "") {
                //redirect to user to link
                alert("Welcome to the " + school);
            } else {
                user = prompt("Please choose your name:", "");
                if (school != "" && school != null) {
                    setCookie("SelectedSchool", school, 365);
                }
            }
        }
    </script>
</body>