2012-11-02 21 views
0

位的jQuery noob,但由於使用了StackOverflow而快速學習。從窗體獲取數據並使用jQuery將數據寫入cookie

任何人都可以給我代碼,將從表單中獲取數據,並使用jQuery將它寫出到cookie(不是會話cookie)。我從這裏裝載的jQuery插件的cookie:

https://github.com/carhartl/jquery-cookie

我也想知道如果我有我的加載代碼時,DOM就緒,如前面PHP我裝餅乾的第一件事。無法讓我的頭繞DOM。

在此先感謝。

確定這裏是當前的代碼,感謝以下回復。我不能讓cookie工作,但警告在未註釋時工作正常:

  <form id="myform"> 
       <fieldset> 
       <h3>Search</h3> 
       <p> 
        <label>Your search *</label> 
        <input type="text" id="mysearch" pattern="[a-zA-Z ]{5,}" maxlength="30" /> 
       </p> 
       <fieldset> 
        <input type="range" id="range" min="1" max="30" value="10" /> 
        <input type="range" id="range2" min="30" max="9000" value="2000" /> 
       </fieldset> 

       <button id="subbtn" type="submit">Submit form</button> 
       <button type="reset">Reset</button> 
       </fieldset> 
      </form> 


     $(function() { 
     // initialize tooltip 
     $(".imgwrap").tooltip({ 
     //$(".tooltip").tooltip({ effect: 'slide'}); 
      // tweak the position 
      offset: [10, 2], 

      // use the "slide" effect 
      effect: 'slide' 

     // add dynamic plugin with optional configuration for bottom edge 
     }).dynamic({ bottom: { direction: 'down', bounce: true } }); 
     }); 

     //this one loads the form validator 
     $("#myform").validator(); 

     //this one for the slider 
     $(":range").rangeinput(); 

    $("#subbtn").on("click", function() { 
    // alert("Cookie!"); 
     $.cookie('Searchitem', $("#mysearch").val(), { expires: 365 }); 
    }); 

任何想法爲什麼我的cookie沒有設置?瀏覽器設置爲接受,我正在用Firebug進行調試。

回答

1

一個持久的cookie是一樣的會話cookie除了它有一個到期日期,所以它掛起。

這將文字輸入的值寫入,在365天后過期餅乾:

<input id="txt" type="text" value="foo" /> 
    <input id="btn" type="button" value="write cookie" /> 

...

$(document).ready(function() { 
     $("#btn").on("click", function() { 
      $.cookie('myCookie', $("#txt").val(), { expires: 365 }); 
     }); 
    }); 

編輯

每更新的新代碼問題:

$(function() { 

     // initialize tooltip 
     $(".imgwrap").tooltip({ 
      //$(".tooltip").tooltip({ effect: 'slide'}); 
      // tweak the position 
      offset: [10, 2], 

      // use the "slide" effect 
      effect: 'slide' 

      // add dynamic plugin with optional configuration for bottom edge 
     }).dynamic({ bottom: { direction: 'down', bounce: true} }); 

     //this one loads the form validator 
     $("#myform").validator(); 

     //this one for the slider 
     $(":range").rangeinput(); 

     $("#subbtn").on("click", function() { 
      // alert("Cookie!"); 
      $.cookie('Searchitem', $("#mysearch").val(), { expires: 365 }); 
     }); 

    }); 
+0

使用您的代碼皮特並添加到原始帖子。還是不能設置cookie,有什麼想法? – Sara44

+0

確保您的所有代碼都包裝在$(document).ready()中。這讓你的例子爲我工作。看到我更新的答案。 –

+0

我會試試這個謝謝。我認爲使用$(function(){與使用$(document).ready()相同,所以如果我得到了一個錯誤的支架,或者有什麼我不在這裏? – Sara44

0

我從您的標記中假設您要使用GET提交表單。您可以使用描述爲here的方法來提取URL字符串值。

然後,它只是一個值設置爲cookie的事:

$.cookie('mycookie', getParameterByName('varname'), {expires: 100}); 

您可以檢查cookie的值,像這樣:

console.log($.cookie('mycookie')); 
+0

感謝馬克,將看你鏈接到的方法。 – Sara44

相關問題