我有一个联系表单,用户可以在其中输入姓名,地址,电话等详细信息。现在,我在表单上有一个复选框(记住我)。每当用户检查时,信息应保存在cookie中,并在同一用户稍后访问时检索。这就是我开始的方式..
<tr><td><input id="mycheck" name="mycheck" data-dojo-type="dijit.form.CheckBox" value="" checked="false" onChange="setCookie" > <label for="mycheck" >Remember me </strong></label></td></tr>
setCookie: function () {
cookie("UserInfo", "cookieValue", { expire: 5 });
},
我如何获取cookie值(这应该是整个表单数据..我需要使用类似byId的东西)... confused ..任何想法??
由于
答案 0 :(得分:2)
请参阅http://dojotoolkit.org/reference-guide/1.7/dojo/cookie.html
如果使用&gt; 1.7你应该引入所需的模块并通过引用使用它(因为它看起来像你在做): 注意不是{expire:X},而是{expire s :x}
<script>
require(["dojo/cookie"], function(cookie){
/* set */
cookie(cookieName, cookieValue, cookieProps);
/* get */
cookie(cookieName);
});
</script>
您可以使用dojo / dom-form模块来提取值并将其保存为一个简单的单行
<form id="myform">
<input type="text" name="field1" value="value1">
<input type="text" name="field2" value="value2">
<input type="button" name="someButton" value="someValue">
</form>
<script>
require(["dojo/dom-form", "dojo/cookie"], function(domForm, dCookie){
dCookie(
"formdata",
domForm.toJson("myId"),
{expires: 5}
);
// The cookie will read: '{"field1":"value1", "field2":"value2"}'
// Note the button was skipped.
// Buttons only gets sent when used as submitbutton + onclick
});
</script>
答案 1 :(得分:1)
将值序列化为JSON,然后在检索时将其撤消:
//Setting the cookie to hold an array of values.
value = {my:"1",cookie:"2"};
dojo.cookie("myCookie", dojo.toJson(value), {expires: 30});
//Retrieving the cookie.
newValues = dojo.fromJson(dojo.cookie("myCookie"));