如何使用cookie,JQuery,Javascript? [英] How to use cookie , JQuery, Javascript?

查看:117
本文介绍了如何使用cookie,JQuery,Javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建一个简单的待办事项列表,我遇到了Cookie的问题。当我删除行 $。cookie(todoDescription + 1,todoDescription); 添加一个任务的按钮工作,并将新任务添加到列表。但是当我离开这一行在网页闪烁,没有任何反应。

I'm currently creating a simple todo list, I'm having a trouble with cookies. When i remove the line $.cookie(todoDescription+1, todoDescription); the button to add a task works, and the new task is added to the list. But when i leave this line in the web page blinks and nothing happens.

   $(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;   
    }

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

I'd appreciate it if someone could help me.

推荐答案

以下是对jQuery cookie使用的描述

Following is the description of Usage of jQuery cookie

创建会话cookie: p>

Create session cookie:

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

建立到期的Cookie,7天后:

Create expiring cookie, 7 days from then:

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

创建有效的cookie,整个网站都有效:

Create expiring cookie, valid across entire site:

$.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,除非您依赖的是默认选项。

Note: when deleting a cookie, you must pass the exact same path, domain and secure options that were used to set the cookie, unless you're relying on the default options that is.

这篇关于如何使用cookie,JQuery,Javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆