如何使用cookie,JQuery,Javascript?

时间:2012-09-27 15:56:49

标签: javascript jquery cookies

我正在创建一个简单的待办事项列表,我遇到了一些问题。当我删除行$.cookie(todoDescription+1, todoDescription);时,添加任务的按钮起作用,新任务将添加到列表中。但是当我在网页上留下这一行时,眨眼间没有任何反应。

   $(document).ready( function() {  

    showCookies();            // to show previous tasks when page is reloaded
    var all =0;

        $('#add_todo').click( function() {                 // button that adds a task

        var cookies = get_cookies_array() ;

        var todoDescription = $('#todo_description').val();   // string from textinput
           var mykey = todoDescription + 1;            //i jst decided to have such key

         $.cookie(todoDescription+1, todoDescription);     //this line doesnt work!

            //add task
            $('.todo_list').prepend(
            '<div class="todo">'
                + '<div>'
                    + '<input type="checkbox" id = "cb" class="check_todo" name="check_todo"/>'
                + '</div>'

                + '<div class="todo_description" contentEditable = "true">'
                    + todoDescription
                + '</div>'

                +'<div id = "delete">' +'<input id = "x" type = "submit" value = "X" onclick = "$.cookie('todoDescription+1',null);$(this).parent().parent().remove();"/>'+ '</div>'
            +'</div>');

           return false;



        }); //end add todo



    });

    function showCookies()
    {

    var cookies = get_cookies_array() ;
        for(var name in cookies) {
        if(name == cookies[name]+1){
             $('.todo_list').prepend(
            '<div class="todo">'
                + '<div>'
                    + '<input type="checkbox" id = "cb" class="check_todo" name="check_todo"/>'
                + '</div>'

                + '<div class="todo_description" contentEditable = "true">'
                    + cookies[name]
                + '</div>'

                +'<div id = "delete">' +'<input id = "x" type = "submit" value = "X" onclick = "$.cookie('name',null);$(this).parent().parent().remove();"/>'+ '</div>'
            +'</div>');
        }
        }

    }
     function get_cookies_array(){ 

          var cookies = { };
            if (document.cookie && document.cookie != '') {

                var split = document.cookie.split(';');
                    for (var i = 0; i < split.length; i++) {
                    var name_value = split[i].split("=");
                    name_value[0] = name_value[0].replace(/^ /, '');
                    cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
        } 
    }
    return cookies;   
    }

如果有人可以帮助我,我会很感激。

1 个答案:

答案 0 :(得分:1)

以下是jQuery cookie的用法说明

创建会话cookie:

$.cookie('the_cookie', 'the_value');

从那时起7天内创建过期的Cookie:

$.cookie('the_cookie', 'the_value', { expires: 7 });

创建过期的Cookie,在整个网站上有效:

$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });

阅读Cookie:

$.cookie('the_cookie'); // => "the_value"
$.cookie('the_cookie', { raw: true }); // => "the_value" not URL decoded
$.cookie('not_existing'); // => null

删除Cookie:

// returns false => No cookie found
// returns true  => A cookie was found
$.removeCookie('the_cookie'[, options]);

注意:删除Cookie时,您必须传递用于设置Cookie的完全相同的路径,域和安全选项,除非您依赖于默认选项。