问题描述
我想在客户端存储表单,在json中的cookie,然后反序列化它回到形式。
我在做什么:
i want to store form on clientside, in json in cookie, after that deserialize it back to form.what i'm doing:
序列化为JSON:
function formToJSON(selector) {
var form = {};
$(selector).find(':input[name]:enabled').each(function () {
var self = $(this);
var name = self.attr('name');
if (name.indexOf('TextBox', 0) == 0) {
if (form[name]) {
form[name] = form[name] + ',' + self.val();
}
else {
form[name] = self.val();
}
}
});
return form;
}
然后在表单更改,我尝试将表单保存到cookie: / p>
then on form change, i'm trying to save form to cookie:
$('#form1 :input').change(function () {
var eba = formToJSON($('#form1'));
$.cookie.set('fo', eba, {json:true});
var a = $.cookie.get('fo',true);
alert(a);
//$.cookie.set('form123', { "ksf": "saf", "tt": "" }, { json: true });
//var b = $.cookie.get('form123', true);
//alert(JSON.stringify(b));
});
在调试器中 - eba是json对象,但alert(a)
注释代码工作,这个json序列化,我得到它从cookie。
但为什么代码不工作的形式???
从jquery.com获取的Cookie插件
in debugger - eba is json object, but alert(a) gives null.commented code works, this json serialized, and i'm gettin it from cookies.but why code doesnt work for form???cookie plugin taken from jquery.com
推荐答案
使用此库来字符串化/解析JSON
Use this library to stringify/parse JSON http://json.org/js.html
请记住,对Cookie的大小限制为4KB, http://support.microsoft.com/kb/306070
remember that there is an approx 4KB size limit on cookies , http://support.microsoft.com/kb/306070
这篇关于序列化形式到json并存储在cookie中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!