简体   繁体   English

我需要选择所有类并像货币 Jquery 一样显示它们

[英]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.

相关问题 可以将一组类存储在内存中,以便在将它们显示在页面上之前可以对它们进行处理吗? (jQuery的) - Can a set of classes be stored in memory, so I can do things to them before I display them on a page? (JQuery) 我希望我的所有选择类,包括稍后可能添加的类使用select2 - I would like all my select classes including those that might be added later to use select2 我需要从本地文件夹中检索所有图像(无论编号和名称如何)并将其显示在我的网站上 - I need to retrieve ALL images (regardless of number and name) from a local folder and display them on my website 在jquery中仅针对一个选择而不是全部 - Targeting only one select in jquery not all of them 如何将数组中的所有元素显示为类 - How to display all elements in an array as classes Jquery 我正在使用jQuery隐藏/显示基于特定类的值,但需要帮助将函数限制在特定范围内 - I am using jQuery to hide/display values based on specific classes but need help confining the function to specific scopes jQuery选择除一个ID之外的所有类 - jQuery select all classes except one ID 我如何从mysqltable中选择所有数据并在html上显示它们 - how can I select all the data from mysqltable and display them on html jQuery冲突问题,但我都需要 - jquery conflict issue but i need both of them 通过jQuery制作div,还需要它们像游戏中一样向上移动 - By Jquery making divs, need them also to move up, like in a game
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM