Firefox - 输入类型号码隐藏小数位 [英] Firefox - input type number hides decimal places

查看:134
本文介绍了Firefox - 输入类型号码隐藏小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在webform上得到了一些输入。我正在计算这里的东西。当我计算的东西(使用JS),我每次使用.toFixed(2)函数。

在IE和Chrome中总是显示2位小数。这就是我想要的。 在Firefox中,小数点后面有隐藏的地方。



这里是我的一些控制示例:< input type =number value =19.00name =taxid =taxmin =0step =0.01class =input_numbertabindex =3>



输出:

- IE:19.00

- FF:19

- Chrome: 19.00

解决方案:

和明显的答案。



http://jsfiddle.net/duuy884a/7 /

 < input type =textmin =0step =0.01name = testid =testvalue =pattern =[0-9] +([,\。] [0-9] +)? /> 
< button onclick =set()>检查< / button>

函数isNumber(n){
return!isNaN(parseFloat(n))&& isFinite的(N);


函数set(){
if(document.getElementById(test).value.length< = 0){return; }

document.getElementById(test)。value = document.getElementById(test)。value.replace(/,/ g,'。');
var fInput = parseFloat(document.getElementById(test)。value);
if(isNumber(fInput)== true){
document.getElementById(test2)。value = checkZeros(document.getElementById(test));



函数checkZeros(oInput){
if(oInput.value ===''){
return;
}

oInput.value = parseFloat(oInput.value).toFixed(2);
oInput.value = oInput.value.toString();

if(oInput.value.indexOf('。')=== -1){
oInput.value = oInput.value +'.00';

while(oInput.value.indexOf('。')> oInput.value.length - 3){
oInput.value = oInput.value +'0';
}

return oInput.value;


解决方案

在答案的评论部分OP的小提琴



更改 test2 输入键入 text 。它会以你想要的方式工作。



< input type =textname =test2id =test2 value =readonly />

I got some inputs on a webform. I'm calculating things here. When I calculate stuff (using JS) I use .toFixed(2) function every time.
In IE and Chrome Numbers are always shown with 2 decimal places. That's what I want. In Firefox decimal places are getting hide.

Here some example of my control: <input type="number" value="19.00" name="tax" id="tax" min="0" step="0.01" class="input_number" tabindex="3">

Output:
- IE: 19.00
- FF: 19
- Chrome: 19.00

Solution:

Solution found and tested with the help the possible duplicate and the marked answer.

http://jsfiddle.net/duuy884a/7/

<input type="text" min="0" step="0.01" name="test" id="test" value="" pattern="[0-9]+([,\.][0-9]+)?" />
<input type="text" min="0" step="0.01" name="test2" id="test2" value="" readonly />
<button onclick="set()">check</button> 

function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

function set (  ){
    if ( document.getElementById("test").value.length <= 0 ){ return; }

    document.getElementById("test").value = document.getElementById("test").value.replace(/,/g, '.');
    var fInput = parseFloat( document.getElementById("test").value );
    if ( isNumber( fInput ) == true ){
        document.getElementById("test2").value = checkZeros( document.getElementById("test") );
    }
}

function checkZeros ( oInput ){
    if ( oInput.value === '') {
        return;
    }

    oInput.value = parseFloat(  oInput.value ).toFixed(2);    
    oInput.value =  oInput.value.toString();

    if ( oInput.value.indexOf('.') === -1) {
         oInput.value =  oInput.value + '.00';
    }
    while ( oInput.value.indexOf('.') >  oInput.value.length - 3) {
         oInput.value =  oInput.value + '0';
    }

    return oInput.value;
}

解决方案

Solution based on the fiddle given by the OP in the comments section of the answer.

Change the test2 input type to text. And it'll work the way you want it to.

<input type="text" name="test2" id="test2" value="" readonly />

这篇关于Firefox - 输入类型号码隐藏小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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