计算元素不起作用

发布于 2025-01-03 17:37:21 字数 1832 浏览 5 评论 0原文

我有一个表,想要计算每个元素,如下所示:

calc-this-cost * calc-this-cost(value of checkbox) = calc-this-total

然后对所有 calc-this-cost 求和并将其放入totalcost div。 这是表:

  <td class="params2">
    <table id="calc-params">
    <tr>
    <td>aaa</td><td class="calc-this-cost">159964</td><td class="calc-this-count">
    <input type="checkbox" name="a002" value="0" onclick="calculate(this);" />
    </td><td class="calc-this-total">0</td>
    </tr>
    <tr>
    <td>bbb</td><td class="calc-this-cost">230073</td><td class="calc-this-count">
    <input type="checkbox" name="a003" value="0" onclick="calculate(this);" />
    </td><td class="calc-this-total">0</td>
    </tr>
    <tr>
    <td>ccc</td><td class="calc-this-cost">159964</td><td class="calc-this-count">
    <input type="checkbox" name="a004" value="1" onclick="calculate(this);" />
    </td><td class="calc-this-total">0</td>
    </tr>
    ........
    </table>
    .......
    </td>
<div id="calc-total-price">TOTAL COST:&nbsp;&nbsp;<span>0</span></div>

我的脚本(在函数计算中)

var totalcost=0;
    $('.params2 tr').each(function(){
        var count=parseFloat($('input[type=checkbox]',$(this)).attr('value'));
        var price=parseFloat($('.calc-this-cost',$(this)).text().replace(" ",""));
        $('.calc-this-total',$(this)).html(count*price);
        totalcost+=parseFloat($('.calc-this-cost',$(this)).text());
    });
    $('#calc-total-price span').html(totalcost);

计算每个元素并将结果放入 calc-this-cost - 工作完美。

但总成本结果为 NaN。为什么?

I have a table and want to calculate each element like:

calc-this-cost * calc-this-cost(value of checkbox) = calc-this-total

Then summ all calc-this-cost and put it to totalcost div.
This is table:

  <td class="params2">
    <table id="calc-params">
    <tr>
    <td>aaa</td><td class="calc-this-cost">159964</td><td class="calc-this-count">
    <input type="checkbox" name="a002" value="0" onclick="calculate(this);" />
    </td><td class="calc-this-total">0</td>
    </tr>
    <tr>
    <td>bbb</td><td class="calc-this-cost">230073</td><td class="calc-this-count">
    <input type="checkbox" name="a003" value="0" onclick="calculate(this);" />
    </td><td class="calc-this-total">0</td>
    </tr>
    <tr>
    <td>ccc</td><td class="calc-this-cost">159964</td><td class="calc-this-count">
    <input type="checkbox" name="a004" value="1" onclick="calculate(this);" />
    </td><td class="calc-this-total">0</td>
    </tr>
    ........
    </table>
    .......
    </td>
<div id="calc-total-price">TOTAL COST:  <span>0</span></div>

My script (in function calculate)

var totalcost=0;
    $('.params2 tr').each(function(){
        var count=parseFloat($('input[type=checkbox]',$(this)).attr('value'));
        var price=parseFloat($('.calc-this-cost',$(this)).text().replace(" ",""));
        $('.calc-this-total',$(this)).html(count*price);
        totalcost+=parseFloat($('.calc-this-cost',$(this)).text());
    });
    $('#calc-total-price span').html(totalcost);

Counting each element and put result to calc-this-cost - work perfect.

But totalcost result NaN. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒗幽 2025-01-10 17:37:21
  1. [一般] 不要使用 parseFloat() 超出需要的范围
  2. [一般] 将重复代码移动到函数
  3. [jQuery] 在上下文和缓存节点 ($row) 上使用 .find()
  4. [一般] 看看如何 String.replace()作品
  5. [一般] 查看 Number.toFixed() 显示浮点数

示例

var totalcost = 0,
    toFloat = function(value) {
        // remove all whitespace
        // note that replace(" ", '') only replaces the first _space_ found!
        value = (value + "").replace(/\s+/g, '');
        value = parseFloat(value || "0", 10);
        return !isNaN(value) ? value : 0;
    };

$('.params2 tr').each( function() {
    var $row = $(this),
        count = toFloat($row.find('.calc-this-count input').val()), 
        price = toFloat($row.find('.calc-this-cost').text()),
        total = count * price;

    $row.find('calc-this-total').text(total.toFixed(2));
    totalcost += total;
});

$('#calc-total-price span').text(totalcost.toFixed(2));
  1. [general] don't parseFloat() more than you need to
  2. [general] move repeating code to functions
  3. [jQuery] use .find() over context and cache nodes ($row)
  4. [general] look at how String.replace() works
  5. [general] look at Number.toFixed() for displaying floats

example

var totalcost = 0,
    toFloat = function(value) {
        // remove all whitespace
        // note that replace(" ", '') only replaces the first _space_ found!
        value = (value + "").replace(/\s+/g, '');
        value = parseFloat(value || "0", 10);
        return !isNaN(value) ? value : 0;
    };

$('.params2 tr').each( function() {
    var $row = $(this),
        count = toFloat($row.find('.calc-this-count input').val()), 
        price = toFloat($row.find('.calc-this-cost').text()),
        total = count * price;

    $row.find('calc-this-total').text(total.toFixed(2));
    totalcost += total;
});

$('#calc-total-price span').text(totalcost.toFixed(2));
别忘他 2025-01-10 17:37:21

console.log() 将解决您的所有问题:

$('.params2 tr').each(function(){
    var count=parseFloat($('input[type=checkbox]',$(this)).attr('value'));
    var price=parseFloat($('.calc-this-cost',$(this)).text().replace(" ",""));
    $('.calc-this-total',$(this)).html(count*price);
    totalcost+=parseFloat($('.calc-this-cost',$(this)).text());
    console.log(count, price, totalcost)
});

在您不理解的地方添加更多日志记录。我不是刚刚告诉你使用日志记录吗? :)

console.log() will solve all your problems:

$('.params2 tr').each(function(){
    var count=parseFloat($('input[type=checkbox]',$(this)).attr('value'));
    var price=parseFloat($('.calc-this-cost',$(this)).text().replace(" ",""));
    $('.calc-this-total',$(this)).html(count*price);
    totalcost+=parseFloat($('.calc-this-cost',$(this)).text());
    console.log(count, price, totalcost)
});

Add more logging where every you don't understand something. Didn't I just tell you to use logging? :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文