位的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進行調試。
使用您的代碼皮特並添加到原始帖子。還是不能設置cookie,有什麼想法? – Sara44
確保您的所有代碼都包裝在$(document).ready()中。這讓你的例子爲我工作。看到我更新的答案。 –
我會試試這個謝謝。我認爲使用$(function(){與使用$(document).ready()相同,所以如果我得到了一個錯誤的支架,或者有什麼我不在這裏? – Sara44