访问动态对象:javascript/jquery

Access dynamic object: javascript/jquery

本文关键字:jquery javascript 动态 对象 访问      更新时间:2023-09-26

我写了一些代码,其中在循环中调用了一个函数,该函数将该循环迭代中的当前对象传递给该函数。

问题是我不知道如何使用该对象,因为它是动态的,我只能访问 obj 参数。

爪哇语

objectifyTableRow(val, i); // Populate object with properties and values

Val 是一个传递给函数 objectifyTableRow 的对象

function objectifyTableRow(objRow, count) { // Create objects for calculations
    var ii = 0;
    $('#ctl00_PageContent_freight_rate_column_chaair_r' + count + " " + 'td').each(function(i, val) { // begin each
        /* Concatenate column name with subcolumn name. example objFobCost.China/Sea Origin,Air */
        if (i < 3) { // columns 0,1,2 in table row
            if (ii < arraySubColumn.length) { // Air, Sea/air, sea subcolumns
                var PropertyName = arrayColumns[0] + arraySubColumn[ii];

                objRow[PropertyName] = parseFloat($(val).html()); // Set key name with var PropertyName 
                ii += 1;
            }
            if (ii == 3) { // Reset counter
                ii = 0;
            }
        } // end of outer if
        else if (i > 2 & i < 6) {
            if (ii < arraySubColumn.length) { // Air, Sea/air, sea subcolumns
                var PropertyName = arrayColumns[1] + arraySubColumn[ii];
                objRow[PropertyName] = parseFloat($(val).html());
                ii += 1;
            }
            if (ii == 3) { // Reset counter
                ii = 0;
            }
        } // end of outer if
        else if (i > 5 & i < 9) {
            if (ii < arraySubColumn.length) { // Air, Sea/air, sea subcolumns
                var PropertyName = arrayColumns[2] + arraySubColumn[ii];
                objRow[PropertyName] = parseFloat($(val).html());
                ii += 1;
            }
            if (ii == 3) { // Reset counter
                ii = 0;
            }
        } // end of outer if
        else if (i > 8 & i < 12) {
            if (ii < arraySubColumn.length) { // Air, Sea/air, sea subcolumns
                var PropertyName = arrayColumns[3] + arraySubColumn[ii];
                ii += 1;
            }
            if (ii == 3) { // Reset counter
                ii = 0;
            }
        } // end of outer if
        else {
            if (ii < arraySubColumn.length) { // Air, Sea/air, sea subcolumns
                var PropertyName = arrayColumns[4] + arraySubColumn[ii];
                           ii += 1;
            }
            if (ii == 3) { // Reset counter
                ii = 0;
            }
        } // end of else
    });                  // end of each loop TD
    beginCalc(objRow);
};

每个对象都传递给beginCalc,因此可以根据ID,键和值进行计算

function beginCalc(obj) {
    // Every obj used is passed to here
    $.each(obj, function(key, element) {
    alert('ID: ' + obj[this.id] + ''n' + 'key: ' + key + ''n' + 'value: ' + element); // Check correct obj id, key and value
});

我这样做的原因是对象存储来自 asp.net 网格的值,我认为为每个网格行创建对象,然后通过选择 obj.key:value * obj.key:value 在代码中进行计算会更干净。而不是使用 getDocumentByElementId。

知道如何在beginCalc函数中通过它们的ID以编程方式访问这些对象吗?

谢谢

你想要

的是 DOM 节点和值之间的连接。尝试此方法(伪代码):

var nodes = $(...select all the nodes...);
nodes.each( function( index, node ) {
    var obj = { node: node };
    beginCalc(obj);
} );

beginCalc() 中,您现在可以使用 obj.node 访问 DOM 节点。您可以使用相同的方法将更多数据放入对象中,以将所有内容保存在一个位置(需要更新的 DOM 节点 + 需要更新的所有数据)。