例題只能保存一次cookie,如果我想保存多次怎么辦?就是我在文本框每次輸入不同的信息,點(diǎn)擊設(shè)置都能保存進(jìn)去?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
? ? <head>
? ? ? ? <title>cookie插件</title>
? ? ? ? <link href="style.css" rel="stylesheet" type="text/css" />
? ? ? ? <script type="text/javascript" src="http://idcbgp.cn/data/jquery-1.8.2.min.js"></script>
? ? ? ? <script src="http://idcbgp.cn/data/jquery.cookie.js" type="text/javascript"></script>
? ? </head>
? ??
? ? <body>
? ? ? ? <div id="divtest">
? ? ? ? ? ? <div class="title">
? ? ? ? ? ? ? ? <span class="fl">cookie插件</span>?
? ? ? ? ? ? ? ? <span class="fr">
? ? ? ? ? ? ? ? ? ? <input id="btnSet" type="button" value="設(shè)置" />
? ? ? ? ? ? ? ? </span>
? ? ? ? ? ? </div>
? ? ? ? ? ? <div class="content">
? ? ? ? ? ? ? ? <span class="fl">郵箱:</span><br />
? ? ? ? ? ? ? ? <input id="email" name="email" type="text" /><br />
? ? ? ? ? ? ? ? <input id="chksave" type="checkbox" />是否保存郵箱
? ? ? ? ? ? </div>
? ? ? ? </div>
? ? ? ??
? ? ? ? <script type="text/javascript">
? ? ? ? ? ? ?$("body").append("<p>" + $.cookie("email") + "</p>");
? ? ? ? ? ? $(function () {
? ? ? ? ? ? ? ? if ($.cookie("email")) {
? ? ? ? ? ? ? ? ? ? $("#email").val($.cookie("username"));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? $("#btnSet").bind("click", function () {
? ? ? ? ? ? ? ? ? ? if ($("#chksave").is(":checked")) {
? ? ? ? ? ? ? ? ? ? ? ? $.cookie("email",$("#email").val(), {
? ? ? ? ? ? ? ? ? ? ? ? ? ? path: "/", expires: 7
? ? ? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else {
? ? ? ? ? ? ? ? ? ? ? ? $.cookie("email",null, {
? ? ? ? ? ? ? ? ? ? ? ? ? ? path: "/"
? ? ? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? });
? ? ? ? </script>
? ? </body>
</html>
2016-05-11
我怎么都能一直保存?刷新5次也沒變啊
2016-03-21
$.cookie('cacheName','cacheValue);
將第二個參數(shù)即緩存的值設(shè)為動態(tài)的變量(將提交的值賦給該變量)就能實(shí)現(xiàn)。