我在jQuery中编写了一个动态表单创建脚本,问题是type = number字段。 随附的选项有:step,min,max,default(= value)和小数(var = dec)。
背景:在我的情况下,小数长度有时与步骤属性索引不同。所以我编写了一个脚本来强制在小数点后填充数字的长度,在校对后它比步长(否则输入函数被破坏):
//get decimal-setting and count digits of step inputs, to set appropriate decimals
jQuery('input[type="number"]').change(function () {
dec = jQuery(this).attr("dec");
step = jQuery(this).attr("step");
decimals = (step.split('.')[1] || []).length;
if (decimals > dec) dec = decimals;
jQuery(this).val(function (i, text) {
return (parseFloat(text)).toFixed(dec);
})
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" id="mynumber" name="mynumber" min="0.5" max="10" step="0.5" dec="5" value="1" />
&#13;
这种方法很好,除了在Firefox中。只是Firefox忽略我的更改功能并设置decimals = step,这通常是正确的方法。
我怎么能强制FF设置小数?