Firefox - 输入类型号码隐藏小数位 [英] Firefox - input type number hides decimal places
问题描述
在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 / 在答案的评论部分OP的小提琴
< 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;
更改 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屋!