货币字段上的 parseInt 返回 NaN


parseInt on currency field returns NaN

我在php/mysql中有一个动态表,我通过复选框显示/隐藏列。下面的代码片段是 javascript 的一部分,用于隐藏和重新计算总列的单元格值。

function toggleVis(button) { 
// Toggle column 
cells = $$('.t'+button.name); 
cells.invoke(button.checked ? 'show' : 'hide'); 
// Recaulculate total 
$$('tr.row').each(function(row) { 
// Initialise to zero 
var total = 0; 
row.down('.total').textContent = total; 
// Sum all visible cells 
row.select('td').each(function(cell) { 
total += cell.visible() ? parseInt(cell.textContent, 10) : 0;
}); 
// Write the total in the total cell 
row.down('.total').textContent = total; 
}); 
}

当表内容只是数字时,这非常有用,但现在我需要创建另一个包含货币值的表。 这会导致总列返回NaN可能是由于 £ 符号。我用下面的代码在 php 中格式化它:

     <tbody>
  <?php do { ?>
    <tr>
      <td><?php echo $row_rsMISource['Source']; ?></td>
      <td><?php echo "£".number_format($row_rsMISource['May'], 2, '.', ','); ?></td>
      <td><?php echo "£".number_format($row_rsMISource['Jun'], 2, '.', ','); ?></td>
      <td><?php echo "£".number_format($row_rsMISource['Jul'], 2, '.', ','); ?></td>
      <td><?php echo "£".number_format($row_rsMISource['Aug'], 2, '.', ','); ?></td>
      <td><?php echo "£".number_format($row_rsMISource['Total'], 2, '.', ','); ?></td>
      </tr>
    <?php } while ($row_rsMISource = mysql_fetch_assoc($rsMISource)); ?>
    </tbody>

这将输出诸如 £10,169.62、£7,053.00 或 £.0.00 之类的值

是否可以在仍然使用上面发布的 js 的同时用货币格式化单元格?

4 + '£4'; //NaN
4 + parseFloat('£4.3'.replace(/[^'d'.]/g, '')); //8.3

这会从字符串中删除非数字字符,并将字符串强制为数字(因此您会得到8.3,而不是"44.3"(。

如果您居住的国家/地区使用逗号而不是句点作为小数点分隔符,请将'.替换为,

[编辑 - 为您的具体示例:]

row.find('td').each(function() { 
    total += $(this).is(':visible') ? parseFloat($(this).text().replace(/[^'d'.]/g, '')) : 0;
});

那里有很多代码更改。