0

我想将数据存储到多维数组或对象中并将数据发送到控制器。我已经在 javascript 中定义了对象,例如

    var dynamicprofile = new Object();


        var certidarry = [];
        var trueorfalse = [];
        dynamicprofile.profilename = "certifications";
        $(".certclass").each(function (index, element) {
            certidarry.push(i);

            if (element.checked == false) {
                trueorfalse.push(false);                   
            }
            else if (element.checked == true) {
                trueorfalse.push(true);
            }
        });
        dynamicprofile.idarray = certidarry;
        dynamicprofile.trueorfalse = trueorfalse;


        dynamicprofile.profilename = "projects";
        var projectidarry12 = [];
        var trueorfalsearry12 = [];
        $(".Projectsclass").each(function (index, element) { 
            projectidarry12.push(index);               
            if (element.checked == false) {
                trueorfalsearry12.push(false);
            }
            else if (element.checked == true) {
                trueorfalsearry12.push(true);
            }
        });
        dynamicprofile.idarray = projectidarry12;
        dynamicprofile.trueorfalse = trueorfalsearry12;

如果看到json内容,那么它只显示最新信息。但它没有显示之前保存的内容。我怎样才能保存内容。

4

2 回答 2

1

问题是您正在覆盖:dynamicprofile.profilename、dynamicprofile.idarray 和 dynamicprofile.trueorfalse。

也许您可以尝试以下解决方案之一:

1.

 var dynamicprofile = new Object();

    var certidarry = [];
    var trueorfalse = [];
    dynamicprofile['certifications'] = {};
    $(".certclass").each(function (index, element) {
        certidarry.push(index);

        if (element.checked == false) {
            trueorfalse.push(false);                   
        }
        else if (element.checked == true) {
            trueorfalse.push(true);
        }
    });
    dynamicprofile['certifications'].idarray = certidarry;
    dynamicprofile['certifications'].trueorfalse = trueorfalse;

    dynamicprofile['projects'] = {};
    var projectidarry12 = [];
    var trueorfalsearry12 = [];
    $(".Projectsclass").each(function (index, element) { 
        projectidarry12.push(index);               
        if (element.checked == false) {
            trueorfalsearry12.push(false);
        }
        else if (element.checked == true) {
            trueorfalsearry12.push(true);
        }
    });
    dynamicprofile['projects'].idarray = projectidarry12;
    dynamicprofile['projects'].trueorfalse = trueorfalsearry12;

--

2.

 var dynamicprofile = [];

 var certidarry = [];
 var trueorfalse = [];

 $(".certclass").each(function (index, element) {
      certidarry.push(index);

      if (element.checked == false) {
          trueorfalse.push(false);                   
      }
      else if (element.checked == true) {
          trueorfalse.push(true);
      }
 });
 dynamicprofile.push({ 
  profilename: "certifications",
  idarray: certidarry,
  trueorfalse: trueorfalse
 });

 var projectidarry12 = [];
 var trueorfalsearry12 = [];
 $(".Projectsclass").each(function (index, element) { 
     projectidarry12.push(index);               
     if (element.checked == false) {
         trueorfalsearry12.push(false);
     }
     else if (element.checked == true) {
         trueorfalsearry12.push(true);
     }
 });
 dynamicprofile.push({ 
  profilename: "projects",
  idarray: projectidarry12,
  trueorfalse: trueorfalsearry12
 });
于 2013-10-24T15:58:31.623 回答
0

你可以通过以下方式做到这一点

 var a=new Array();
    for(vr i=0;i<10;i++)
    {
     a[i]=new Array();
    a[i][0]='value1';
    a[i][1]='value2';
    }
于 2013-10-24T15:28:06.070 回答