我很困惑:下面的代码发生了什么
var ProductFeatures = [];
for (var i = 1; i < 3; i++) {
ProductFeatures.push({
Guid: $('#FeatureListTable tr').eq(i).attr('id'),
Value: $('#FeatureListTable td:nth-child(5)').eq(i-1).val(),
Remark: $('#FeatureListTable td:nth-child(6) input').eq(i-1).val()
});
}
当我注释掉“值:”行时,我在“备注”字段中得到的结果与没有注释时不同
// Value: $('#FeatureListTable td:nth-child(5)').eq(i-1).val(),
为什么会发生这种情况?
提前致谢,朱利安
最佳答案
不知道为什么 Remark
当您注释掉 Value
时,值会改变行,但是这一行:
Value: $('#FeatureListTable td:nth-child(5)').eq(i-1).val(),
...只会返回 null
或undefined
(我不记得是哪一个),因为它试图获取 value
的<td>
而不是表单输入。
使用 jQuery 的 .map()
可以更好地构建这种数据结构。方法。
关于循环中的 Jquery 选择器给出的结果不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4598489/