[英]I need to select all classes and display them like currency Jquery
I need to select all classes and display them like currency .我需要选择所有类并像货币一样显示它们。 Also it need to be displayed like a function .它也需要像函数一样显示。 It need to update everytime I add some data .每次我添加一些数据时都需要更新。 I tried a lot of stuff , nothing helped .我尝试了很多东西,没有任何帮助。
I now I need to make it with each .我现在需要用每个. I got an error toFixed is not a function我收到一个错误toFixed is not a function
$('.dashboard__currency').each(function(){
return $(this).text().toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
});
Is the any solutions ?有什么解决办法吗?
There's two issues here.这里有两个问题。 Firstly the error you get is because text()
returns a string, yet toFixed()
is a method of Number types.首先你得到的错误是因为text()
返回一个字符串,而toFixed()
是一个 Number 类型的方法。 The second problem is that a return
in an each()
statement does not update the content of the element.第二个问题是each()
语句中的return
不会更新元素的内容。
To fix this you can use parseFloat()
to convert the string to a number, and you can also provide a function to text()
which then returns the new value.要解决此问题,您可以使用parseFloat()
将字符串转换为数字,还可以向text()
提供一个函数,然后返回新值。 This negates the need for the explicit each()
call.这不需要显式的each()
调用。 Try this:尝试这个:
$('.dashboard__currency').text((i, t) => parseFloat(t).toFixed(2).replace(/\\d(?=(\\d{3})+\\.)/g, '$&,'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <span class="dashboard__currency">10205.99</span><br /> <span class="dashboard__currency">15232323.00</span><br /> <span class="dashboard__currency">193235</span>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.