2012-09-27 68 views
1

我目前正在創建一個簡單的待辦事項列表,我遇到了Cookie問題。當我刪除行$.cookie(todoDescription+1, todoDescription);添加任務的按鈕工作,並且新任務被添加到列表中。但是,當我在網頁中留下這一行閃爍並沒有任何反應。如何使用cookie,JQuery,Javascript?

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

如果有人能幫助我,我將不勝感激。

+0

Firebug告訴你什麼? –

回答

1

以下是jQuery的餅乾的用法說明

創建會話cookie:

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

創建過期餅乾,從那以後7天:

$.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的安全選項,除非你依靠的是默認選項。