I want to save the form on clientide, in json in the cookie, after that deserialize it back to the form. What am I doing:
serialization in 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; }
then when changing the form, I try to save the form in a 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)); });
in the debugger - eba is a json object, but alert (a) indicates null. commented code works, this json series is serialized and I get it from cookies. but why does the code not work for the form ??? cookie plugin taken from jquery.com
json javascript jquery serialization cookies
Jack Malkovich Mar 28 '11 at 12:05 2011-03-28 12:05
source share