JQuery addition from .text()

JQuery addition from .text()

本文关键字:text from addition JQuery      更新时间:2023-09-26

我有一个变量计数设置为零。

var count = 0;

然后我运行.each()循环来更新计数。text()参数将始终返回一个文本编号。

$(this).find('.outersubtopic').each(function(idx)
{
    var temp = $(this).text();
    count += temp;                          
});
alert(count);

当我点击警报时,它总是显示0008,而不是只有8。

如何将.text()转换为实数?

$(this).find('.outersubtopic').each(function(idx)
{
    var temp = $(this).text();
    count += window.parseInt(temp);                          
});
alert(count);

window.parseInt()应该为您完成此操作。

您必须将其转换为数字

$(this).find('.outersubtopic').each(function(idx)
{
    var temp = $(this).text();
     count += Number(temp);                          
});
alert(count);

尝试使用parseInt

  $(this).find('.outersubtopic').each(function(idx)
    {
        var temp = $(this).text();
        count += temp;                          
    });
    alert(parseInt(count));
 count += 1 * temp; // quick way to change string to number