gpt4 book ai didi

javascript - 乘以单元格的值不返回任何内容

转载 作者:行者123 更新时间:2023-11-29 21:20:11 26 4
gpt4 key购买 nike

我有一个函数可以遍历行并捕获特定单元格中的值。它成功地做到了这一点,我什至能够返回那些单独的值。但是,当我尝试对它们进行倍增时,它什么也不返回。这是我的 JSFiddle

这是我的功能:

$('#' + value + ' .totalRacks').each(function() {
$.each(this.cells, function() {
var index = $(this).index() + 1;
var prevRow0 = $(this).closest('tr').prev().find('td:nth-child(' + index + ')');
var prevRow1 = $(this).closest('tr').prev().prev().prev().find('td:nth-child(' + index + ')');
if (!isNaN(parseInt(prevRow0.text()))) {
var td1 = parseInt(prevRow0.text());
}
if (!isNaN(parseInt(prevRow1))) {
var td2 = parseInt(prevRow1);
}
var product = parseInt((td1 * td2));
$(this).html(product);
});
});

最佳答案

你有几个逻辑问题。首先,您可以使用单个 each() 调用循环遍历 td 单元格,并且您错过了 text() 的检索>prevRow1。如果你解决了这些问题,代码就可以工作了。您还可以通过将值默认为 0 来整理 isNaN 检查。试试这个:

$('#' + value + ' .totalRacks td:not(:first, :last)').each(function() {
var index = $(this).index() + 1;
var prevRow0 = $(this).closest('tr').prev().find('td:nth-child(' + index + ')');
var prevRow1 = $(this).closest('tr').prev().prev().prev().find('td:nth-child(' + index + ')');

var td1 = parseInt(prevRow0.text(), 10) || 0;
var td2 = parseInt(prevRow1.text(), 10) || 0;
var product = parseInt(td1 * td2);
$(this).html(product);
});

Updated fiddle

关于javascript - 乘以单元格的值不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38725257/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com