日期:2014-05-16  浏览次数:20491 次

jquery cookie 怎样存放数组对象,还有其他方法吗?
function Menu(id,scopeid,parentid,sortname,sortpath,remarks,orderid){  //自定义对象

this.id = id;
this.scopeid = scopeid;
this.parentid = parentid;
this.sortname = sortname;
this.sortpath = sortpath;
this.remarks = remarks;
this.orderid = orderid;

}

function initMenus(xmlText){      //保存所有自定义对象到cookie

if(xmlText){
var menuitems  = new Array();
var tableitems = tableParser(xmlText); //返回数组对象
for(var i=0;i<tableitems.length;i++){

var table = tableitems[i];
var id = nodeParser(table,'Id');         //抽取属性实例化Menu
var scopeid = nodeParser(table,'ScopeId');
var parentid = nodeParser(table,'ParentId');
var sortname = nodeParser(table,'SortName');
var sortpath = nodeParser(table,'SortPath');
var remarks = nodeParser(table,'Remarks');
var orderid = nodeParser(table,'OrderID');
var menu = new Menu(id,scopeid,parentid,sortname,sortpath,remarks,orderid);
menuitems.push(menu);
}
$.cookie('items',menuitems);

}

}

其中:$.cookie('items',menuitems); 无法将menuitems对象保存进cookie,各位大虾,请指教

------解决方案--------------------
cookie的大小是有限制的,小心cookie自动丢失
------解决方案--------------------
恩 正如1楼说的,cookie适合小量不重要的数据。
不过搂住要往cookie里存object还是可以实现的,不过还要依赖于其他插件json2.js,
下面这个例子要jquery.js, jquery.cookie.js, json2.js

        $(function () {
            var obj = new Object();
            obj.ID = 1;
            obj.Name = "sddsd";
            var objString = JSON.stringify(obj); //JSON 数据转化成字符串
            $.cookie('myCookie', objString );

            var myCookie = $.cookie('myCookie');
            var newObject = JSON.parse(myCookie); //字符串转化成JSON数据
        });