0

我有一个数组对象,需要添加另一个数组对象。所以我有需要将表中的行作为数组添加到该对象的对象的详细信息。我在 stackoverflow 上尝试了一些建议,但似乎没有一个有效,我不确定这与表是由 js 创建的事实有关。

// Adding Cosignment number
$('#parcel-overview').append(
 // Add main tables which will display the added packages
 '<table border="1">' +
 '<thead><tr><th>Packages</th><th>Weight</th> <th>Vol Weight</th><th>Charge Weight</th> <th>Price</th></tr></thead><tbody id="parcels-added-overview"></tbody> ' +
 '</table>'
 );

for (var i = 0; i < packageNum; i++) {

    var ii = (i + 1).toString();

    // Working out volemetric weight
    wei = $('#added-parcels #weighting-' + ii + ' input').val();
    len = $('#added-parcels #lengthing-' + ii + ' input').val();
    wid = $('#added-parcels #widthing-' + ii + ' input').val();
    hei = $('#added-parcels #heighting-' + ii + ' input').val();

    //Calculating Volumetric weight
    tot = ((len * wid * hei) / 5000).toFixed(1);

    pri = (tot * 23).toFixed(2);

    chr = (tot * 12).toFixed(2);
    $('#parcels-added-overview').append(
   '<tr>' +
     '<td class="par-id">' + (i + 1).toString() + '</td>' +
     '<td class="par-weight">' + wei.toString() + ' kg\'s</td>' +
     '<td class="par-vol-weight">' + tot.toString() + ' kg\'s</td>' +
     '<td class="par-charge-weight">R ' + chr.toString() + '</td>' +
     '<td class="par-price">R ' + pri.toString() + ' </td>' +
   '</tr>'
    );
}

然后我想将该表的值添加到对象数组中,然后将其添加到主对象数组中。

 var parcelObj = new Object();

    $.each($('#parcels-added-overview tr'),function (index) {

        parcelObj.parcelId = $(this).children('.par-id').text();
        parcelObj.totalWeight = $(this).children('.par-weight').text();
        parcelObj.volWeight = $(this).children('.par-vol-weight').text();
        parcelObj.chargeWeight = $(this).children('.par-charge-weight').text();
        parcelObj.overallPrice = $(this).children('.par-price').text();

        parcelsArr.push(parcelObj);

    });

    consignmentObj.parcels = parcelsArr;

    consignmentsArr.push(consignmentObj);

我可能是一个 n00b ,但这段代码(虽然我认为它相当冗长)应该可以工作。

$(this).children 是否没有直接识别它正在迭代的 each() 行?

当我添加 console.log(consignmentsArr); 我得到了对象中的数组,但包裹对象的值只是重复表的最后一行。

1: Object
  deliName: ""
  deliStreet: ""
  docType: "Document"
  insurance: "None"
  parcels: Array[2]
    0: Object
      chargeValue:"R34.43"
      overallPrice:"R43.54"
      parcelId:"2"
      totalWeight:"65 kg's"
      volWeight:"63 kg's"
    1: Object
      chargeValue:"R34.43"
      overallPrice:"R43.54"
      parcelId:"2"
      totalWeight:"65 kg's"
      volWeight:"63 kg's"

为什么我不能将第一行值添加到 parcels[0]?

谢谢

4

1 回答 1

1

尝试在函数内声明 parcelObj 对象。

    $.each($('#parcels-added-overview tr'),function (index) {

        var parcelObj = new Object();

        parcelObj.parcelId = $(this).children('.par-id').text();
        parcelObj.totalWeight = $(this).children('.par-weight').text();
        parcelObj.volWeight = $(this).children('.par-vol-weight').text();
        parcelObj.chargeWeight = $(this).children('.par-charge-weight').text();
        parcelObj.overallPrice = $(this).children('.par-price').text();

        parcelsArr.push(parcelObj);

    });

    consignmentObj.parcels = parcelsArr;

    consignmentsArr.push(consignmentObj);
于 2013-09-18T07:54:44.543 回答