货币字段上的parseInt返回NaN

gar*_*ary 0 javascript php

我在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; 
}); 
}
Run Code Online (Sandbox Code Playgroud)

当表内容只是数字时,这很有效,但我现在需要创建另一个货币值为的表.这会导致总列返回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>
Run Code Online (Sandbox Code Playgroud)

输出值为10,169.62英镑,7,053.00英镑或0.00英镑

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

Utk*_*nos 5

4 + '£4'; //NaN
4 + parseFloat('£4.3'.replace(/[^\d\.]/g, '')); //8.3
Run Code Online (Sandbox Code Playgroud)

这将从字符串中删除非数字字符,并将字符串强制转换为数字(所以你得到8.3,而不是"44.3").

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

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

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

那里有相当多的代码更改.