使用jquery替换将数字替换为字符串
我的问题与此主题此处有关。
我需要一个带有字符串的数字的 jquery 替换函数,但更好。
我的 php 返回此格式的数字,例如 1.27
根据范围,此数字应替换为与范围匹配的特定字符串:
示例:
数字 1.27 在 1.0 的“活动”范围内,并且1.5,所以它的替换将是字符串“active”。 1.51 的值将位于从 1.5 到 2.0 的“不太活跃”范围内,并且替换字符串将是“不太活跃”字符串。
任何想法都非常感激
My question is related to this topic here.
I would need a jquery replacement function for a number with a string as well, but much finer though.
My php returns a number in this format eg 1.27
Depending on a range this number should be replaced by a certain string that matches a range:
Example:
The number 1.27 is in the "active" range of 1.0 and 1.5, so the replacement for this would be the string "active".
The value of 1.51 would be in the "less active" range from 1.5 to 2.0 and the replacement string would be the "less active" string.
Any ideas are much appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可以使用 jQuery 访问页面中的文本,但其余代码是纯 JavaScript。
您可以使用正则表达式来查找数字,然后使用函数来确定用什么来替换它,如下所示:
Accessing the text in the page can be done using jQuery, but the rest of the code is plain Javascript.
You can use a regular expression to find the number, then use a function that determines what to replace it with, something like this:
不幸的是你不能用一行代码来做到这一点。您必须执行类似以下操作:
请参阅上面的 jsFiddle 上的示例
Unfortunately you cannot do this with a single line of code. You'd have to do something like the following:
See the above example on jsFiddle
我不明白 jQuery 与它有什么关系,但这真的非常简单......
您可能想要一本关于编程的好书,这真的不应该是一个问题。
I dont understand what jQuery has to do with it, but this is pretty simple really...
You might want to get a good book about programming, this really shouldnt be an issue.