gpt4 book ai didi

jquery - 添加数字后继续得到 NaN - jQuery

转载 作者:行者123 更新时间:2023-12-01 06:39:36 25 4
gpt4 key购买 nike

我有这个脚本:

var foo=0;

$('#container .field').each(function(){
var item = $(this);
foo += parseInt(item.val());
})

console.log(foo);

但是每次,当我记录 tot 变量时,它都会显示 NaN。 .field 类被赋予输入字段。

有人可以帮助我吗?

最佳答案

如果您的某个字段中有一个值无法解析为整数,则可能会发生这种情况。

参见here解决方案:

// Note: add 10 as the radix parameter to indicate you want
// base-10 integers.
foo += parseInt(item.val(), 10) || 0;

这将有效地跳过无法解析的值。

关于jquery - 添加数字后继续得到 NaN - jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6847762/

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