问题描述
我想强制< input type =numberstep =0.01/>
总是有两位小数来输入会计数据。 / p>
我已经设法使用JavaScript来完成这项工作。
document.getElementById('input')。addEventListener('change',force2decimals); function force2decimals(event){event.target.value = Number(Math.round(event.target.value * 100)/ 100).toFixed(2);}
< input type =numberstep =0.01id =inputvalue =1.00/>
p>
有什么方法可以处理这个本地?
我知道步骤 code>属性,重复答案不回复我的问题。
解决方案可以这样做:
document.getElementById('input' ).addEventListener('change',force2decimals);函数force2decimals(event){event.target.value = parseFloat(event.target.value).toFixed(2);}
< input type =numberstep =0.01id =inputvalue =1.00/ >
没有办法在本地 HTML5。
I'd like to force an <input type="number" step="0.01" />
to always have 2 decimals to enter accounting data.
I've managed to do that using JavaScript
document.getElementById('input').addEventListener('change', force2decimals);
function force2decimals(event) {
event.target.value = Number(Math.round(event.target.value * 100) / 100).toFixed(2);
}
<input type="number" step="0.01" id="input" value="1.00" />
Is there any way to handle this natively?
I know about the step
attribute, the "duplicate answer" doesn't reply to my question.
解决方案 It could be done by this:
document.getElementById('input').addEventListener('change', force2decimals);
function force2decimals(event) {
event.target.value = parseFloat(event.target.value).toFixed(2);
}
<input type="number" step="0.01" id="input" value="1.00" />
There is no way to do this "natively" in HTML5.
这篇关于原生强制输入数字小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!