无法将结果数字格式化为十进制形式
为什么这不将 #total 显示为小数? class="total" 中的输入值是以 2 位十进制格式存储该值的函数结果。显示行总计的函数工作正常,但是当我使用下面的代码在 qty 字段更改时对行进行总计时,垫子是正确的并且更改正确发生,但它采用 int 样式而不是货币格式(2 位小数)的地方)。我也尝试过 Precision,没有任何改变。
$('[name^=qty]').change(function(){
var sum = 0;
$('input.total').each(function() {
sum += $(this).val();
});
$('#total').val(sum).toFixed(2);
});
以下是对每行进行总计的代码
function Tot(x,price,y) {
var total = CurrencyFormatted(x*price);
var v = document.getElementById(y);
v.setAttribute('value',total);}
所有这些函数都运行良好,正确总计并按预期更新,但总计字段中的小数位除外。
下面是我为每一行使用的 html,然后是总计
<td>$<input id="t1" disabled="disabled" size="8" value="0.00" class="total" />
...
<input disabled="disabled" id="total" name="total" size="8" value="0.00" />
css 中没有任何内容会改变类或表结构中数字的格式。我完全不知所措,但希望有人能看到我看不到的东西。先感谢您。
Why would this not show #total as a decimal?
The value of the inputs in class="total" are a function result that stores the value in 2 decimal format. The function to display the row totals works fine but when I use the below code to total the rows when the qty field changes, the mat is correct and the change takes place correctly, but it in an int style not a currency format (2 decimal places). I have tried toPrecision as well with no change.
$('[name^=qty]').change(function(){
var sum = 0;
$('input.total').each(function() {
sum += $(this).val();
});
$('#total').val(sum).toFixed(2);
});
Here is the code that totals each row
function Tot(x,price,y) {
var total = CurrencyFormatted(x*price);
var v = document.getElementById(y);
v.setAttribute('value',total);}
All of these functions run just fine, totaling correctly and updating as expected, with exception to the decimal places in the grand total field.
Below is the html that I am using for each row and then the total
<td>lt;input id="t1" disabled="disabled" size="8" value="0.00" class="total" />
...
<input disabled="disabled" id="total" name="total" size="8" value="0.00" />
There is nothing in the css that would change the format of the number either in the class or the table structure. I am at a complete loss, but hopefully someone will see something I don't. Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑,因为我很忙,所以我没有解释我的答案,格雷格·佩蒂特做了,所以这里是解释
你试图将 toFixed() 链接到不是数字的东西。此外,您没有将检索到的值转换为浮点数。 JS 可能已经处理了后者,但最好按照示例通过 parseFloat 运行它
EDIT, since i was busy i didn't explain my answer, Greg Pettit did, so here's the explanation
You were trying to chain toFixed() to something that's not a number. Also, you weren't converting your retrieved values to floats. JS may have handled the latter, but better to run it through parseFloat as per sample
您可以使用 parseFloat 函数来执行此操作:
有关更多示例:
JQuery 类型示例
You can use the parseFloat function to do it:
for more examples:
JQuery Type examples
val
返回一个String
对象,而不是数字。在调用toFixed(2)
之前,您需要调用parseFloat
或转换为Number
val
returns aString
object, not a number. you'll need to callparseFloat
or cast toNumber
before callingtoFixed(2)