gpt4 book ai didi

javascript - jquery 没有按预期计算总和

转载 作者:行者123 更新时间:2023-11-30 18:23:33 25 4
gpt4 key购买 nike

我开始使用这个解决方案,但我似乎无法让它工作

jQuery calculate sum of values in all text fields

这是我的 html 示例

<table>
<tbody>
<tr id="tr_affiliation_15159">
<td class="right col_aff_amount">15.00</td>
</tr>
<tr id="tr_affiliation_15189">
<td class="right col_aff_amount">15.00</td>
</tr>
</tbody>
<tfoot>
<tr>
<td class="total"></td>
</tr>
</tfoot>
</table>

这是我的javascript函数

function removeRow(id) {
$("#tr_affiliation_" + id).fadeOut("slow", function() {
$("#tr_affiliation_" + id).remove();

// recalc total
var total = 0;

$('.col_aff_amount').each(function() {
// i have tried .text(), and .html() as well as .val() but it doesn't seem to make any difference
var val = parseFloat($(this).val());
console.log(val);
total += val;
});

$("td.total").html(parseFloat(total).toFixed(2));

}

我的控制台日志只显示一长串 NaN,为什么它不能提取数字?

最佳答案

你应该使用 text() 来处理 input 和 textarea 等表单元素以外的元素,同时在你进行计算时,在变量 val 之前定义一个整数值计算:

$(document).ready(function(){
// ...
var val = 0;
val = parseInt($(this).text(), 10);
// ...
})

DEMO

关于javascript - jquery 没有按预期计算总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11480464/

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