访问动态对象:javascript/jquery
Access dynamic object: javascript/jquery
我写了一些代码,其中在循环中调用了一个函数,该函数将该循环迭代中的当前对象传递给该函数。
问题是我不知道如何使用该对象,因为它是动态的,我只能访问 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 节点 + 需要更新的所有数据)。
相关文章:
- jQuery/Javascript.toggleClass/.classList.toggle的特殊行为
- 学生搜索项目jquery/javascript
- jQuery/JavaScript在线公文包表单-打印样式表
- 如何在页面加载中使用Jquery/Javascript确定img源
- DataTables合并了两个独立表中的jQuery/Javascript函数
- 在 Jquery/Javascript 中使用多个 OR (||) 运算符时如何设置变量
- 如何在jquery+javascript中只在输入框中输入数字(使用regex)
- 在Jquery/Javascript中的Array中插入Textbox值
- 使用Jquery/Javascript替换CSS属性
- Jquery/Javascript:通过按下按钮显示数组中的随机图像
- 选中复选框的Jquery/Javascript问题使用输入框操作将行从一个表添加到另一个表
- 在for循环中未获取JQuery/JavaScript对象值
- 使用jQuery/Javascript在Facebook上发送消息
- jQuery/Javascript foreach If/Else |如果为空(不执行任何操作)Else(显示forea
- 使用jQuery+Javascript识别一个空文本节点
- jquery/javascript字符串中的撇号
- jQuery/Javascript在内容可编辑的当前段落后插入元素
- 在页面加载时激活jQuery/Javascript函数
- 如何创建php函数或任何脚本/jquery Javascript,使我能够获取与复选框关联的值并添加它们
- Jquery/Javascript使用IF/ELSE语句更改img SRC