HTML5 Number Input - Always show 2 decimal places

后端 未结 15 2156
太阳男子
太阳男子 2020-11-28 05:43

Is there\'s any way to format an input[type=\'number\'] value to always show 2 decimal places?

Example: I want to see 0.00 instead of

15条回答
  •  醉酒成梦
    2020-11-28 06:27

    The top answer gave me the solution but I didn't like that the user input was changed immediately so I added delay which in my opinion contributes to a better user experience

    var delayTimer;
    function input(ele) {
        clearTimeout(delayTimer);
        delayTimer = setTimeout(function() {
           ele.value = parseFloat(ele.value).toFixed(2).toString();
        }, 800); 
    }

    https://jsfiddle.net/908rLhek/1/

提交回复
热议问题