toFixed()在文本字段中简单使用/解释 [英] toFixed() Simple use/explanation in text field

查看:116
本文介绍了toFixed()在文本字段中简单使用/解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如我所知,我避免在这里提出这个问题.在过去的几天中,我花了一些时间试图找到解决方案/弄清楚toFixed()方法的工作方式.我已经在该网站上阅读了很多问题,并在其他网站上阅读了教程,但仍然不明白.

我有几个带有".entry"类的文本字段.这里应该有一美元.当人们键入以下内容(示例)时:

1.2
5
6.35
8.

我需要他们更改为:

1.20
5.00
6.35
8.00

换句话说,添加尾随零.我知道这是通过toFixed()方法完成的,但我完全不知所措.我无法使它正常工作.

我有一个脚本,发现该脚本将页面上其他位置的DIV中的所有文本字段总计在一起,并且我注意到它使用了toFixed()方法:

$("#total").html(sum.toFixed(2).replace(/(^\d{1,3}|\d{3})(?=(?:\d{3})+(?:$|\.))/g, '$1,'));
}

我尝试在此处使用相同的代码,以便零可以显示在文本字段中:

$('.entry').keyup(function(){
   var str = this.value.replace(/(^\d{1,3}|\d{3})(?=(?:\d{3})+(?:$|\.))/g, '$1');
   if (str!=this.value) this.value = str; 
});

它不起作用.

我对Jquery和Javascript还是陌生的,所以我意识到我可能缺少明显的东西.我阅读过的大多数教程都在代码中设置了变量,然后使用"document.write"显示具有正确零个数的变量:

示例:

document.write( 1.1.toFixed(2) + '<br>' );

但这不是我想要的.我需要它显示在文本字段中.

提前谢谢!

解决方案

几件事:

  1. 使用change事件而不是keyup.如果使用keyup,则每次用户尝试键入内容时,文本都会更改,这是令人讨厌的用户体验.
  2. 考虑将类型为numberinput与步长为0.1一起使用.

考虑到这些,我会做这样的事情:

$('.entry').change(function(){
   // parse the typed value as a floating point number
   var num = parseFloat(this.value);

   // ensure there are two decimal places
   this.value = num.toFixed(2);
});

请注意,如果用户键入的东西的小数点后两位以上,则该值将四舍五入.

示例: http://jsfiddle.net/jndt1e02/

I've avoided asking this question here as I know many have in the past. I've spent some time during the last few days trying to find a solution/figure out how the toFixed() method works. I've read a lot of questions on this site and tutorials on others but I'm still not getting it.

I have several text fields with the class, ".entry". A dollar amount is supposed to go here. When people type the following (examples):

1.2
5
6.35
8.

I need them to change to:

1.20
5.00
6.35
8.00

In other words, add the trailing zeros. I know this is accomplished through the toFixed() method but I'm completely at a loss. I can't get it to work.

I have a script I found that totals all the text fields in a DIV elsewhere on the page and I notice that it uses the toFixed() method:

$("#total").html(sum.toFixed(2).replace(/(^\d{1,3}|\d{3})(?=(?:\d{3})+(?:$|\.))/g, '$1,'));
}

I tried using that same code here so the zeros could display in the text field:

$('.entry').keyup(function(){
   var str = this.value.replace(/(^\d{1,3}|\d{3})(?=(?:\d{3})+(?:$|\.))/g, '$1');
   if (str!=this.value) this.value = str; 
});

It doesn't work.

I'm new to Jquery and Javascript so I realize I'm probably missing something obvious. Most of the tutorials I've read set the variable in the code and then use "document.write" to display the variable with the correct number of zeros:

Example:

document.write( 1.1.toFixed(2) + '<br>' );

But this isn't what I'm looking for. I need it to show up in the text field.

Thanks in advance!

解决方案

A few things:

  1. Use the change event instead of keyup. If you use keyup, the text wil change every time the user tries to type something, which is an annoying user experience.
  2. Consider using an input of type number with a step of 0.1.

With those in mind, I'd do something like this:

$('.entry').change(function(){
   // parse the typed value as a floating point number
   var num = parseFloat(this.value);

   // ensure there are two decimal places
   this.value = num.toFixed(2);
});

Note that if the user types something with more than two decimal places, the value will be rounded.

Example: http://jsfiddle.net/jndt1e02/

这篇关于toFixed()在文本字段中简单使用/解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆