Make an html number input always display 2 decimal places

后端 未结 8 514
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 05:09

I\'m making a form where the user can enter a dollar amount using an html number input tag. Is there a way to have the input box always display 2 decimal places?

8条回答
  •  爱一瞬间的悲伤
    2020-11-30 06:03

    The accepted solution here is incorrect. Try this in the HTML:

    onchange="setTwoNumberDecimal(this)" 
    

    and the function to look like:

     function setTwoNumberDecimal(el) {
            el.value = parseFloat(el.value).toFixed(2);
        };
    

提交回复
热议问题