【问题标题】:Counting elements doesn't work计数元素不起作用
【发布时间】:2012-02-08 18:00:24
【问题描述】:

我有一张表格,想计算每个元素,例如:

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。为什么?

【问题讨论】:

    标签: javascript jquery counting


    【解决方案1】:
    1. [general] 解析Float() 的次数不要超过您需要的次数
    2. [general] 将重复代码移至函数
    3. [jQuery] 在上下文和缓存节点上使用 .find() ($row)
    4. [general] 看看 String.replace() 是如何工作的
    5. [general] 查看 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));
    

    【讨论】:

      【解决方案2】:

      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)
      });
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-17
        • 1970-01-01
        • 1970-01-01
        • 2018-09-23
        • 1970-01-01
        相关资源
        最近更新 更多