简体   繁体   English

toFixed()在文本字段中简单使用/解释

[英]toFixed() Simple use/explanation in text field

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. 在过去的几天中,我花了一些时间试图找到解决方案/弄清楚toFixed()方法的工作方式。 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". 我的类“ .entry”有几个文本字段。 A dollar amount is supposed to go here. 这里应该有一美元。 When people type the following (examples): 人们键入以下内容(示例)时:

1.2 1.2
5
6.35 6.35
8. 8。

I need them to change to: 我需要他们更改为:

1.20 1.20
5.00 5.00
6.35 6.35
8.00 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. 我知道这是通过toFixed()方法完成的,但我完全不知所措。 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: 我有一个脚本,发现该脚本总计了页面上其他位置的DIV中的所有文本字段,并且我注意到它使用了toFixed()方法:

$("#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. 我是Jquery和Javascript的新手,所以我意识到我可能缺少明显的东西。 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: 我阅读过的大多数教程都在代码中设置了变量,然后使用“ document.write”显示具有正确零个数的变量:

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 . 使用change事件代替keyup If you use keyup , the text wil change every time the user tries to type something, which is an annoying user experience. 如果使用keyup ,则每次用户尝试键入内容时文本都会更改,这是令人讨厌的用户体验。
  2. Consider using an input of type number with a step of 0.1 . 考虑使用input的类型number与的步骤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/ 示例: http //jsfiddle.net/jndt1e02/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM