English 中文(简体)
我怎么能够使“超”5号数字现场显示零线。
原标题:How can I make the HTML5 number field display trailing zeroes?

我有一个领域:

<input type= number  />

I d like to punch in 0.50 without it “correcting it” to 0.5, so it would display 0.50.

最佳回答

我在此问题上几乎没有什么作用,并看着眼光。 它说,它必须是有效的浮动点。

The best representation of the number n as a floating point number is the string obtained from applying the JavaScript operator ToString to n.

这意味着,这一格式将始终与评估数字一致,然后使用Java文本,以达到这一数目。 因此,当时没有任何拖车。

因此,你不得不诉诸于 Java。 这一直截了当,因为document.getElementById(numInput ) = 0.50 ; 仍然得到更正,以0.5,因此可在onchange上启动的验证编号,在可以防止缺席行动的情况下,在内部启动。

This is the best solution I could come up with... it s a bit of a hack, and will need a bit of tweaking for robustness, but hopefully it ll do what you want:

var numInput = document.getElementById( numInput );
numInput.addEventListener( keypress , function () {
    this.setAttribute( type ,  text );
});
numInput.addEventListener( click , function () {
    this.setAttribute( type ,  number );
});

因此,如果用户希望通过打字进入编号,则将输入类型移至案文,但当他们点击时,用户会将其改成数字。

如果你总是想要拖车,不管用户类型如何,那么你就可以这样做:

var numInput = document.getElementById( numInput );
numInput.addEventListener( blur , function () {
    if (this.value ===   ) {
        return;
    }
    this.setAttribute( type ,  text );
    if (this.value.indexOf( . ) === -1) {
        this.value = this.value +  .00 ;
    }
    while (this.value.indexOf( . ) > this.value.length - 3) {
        this.value = this.value +  0 ;
    }
});
numInput.addEventListener( focus , function () {
    this.setAttribute( type ,  number );
});

<><>Edit>: 我认为,第二种解决办法更符合用户的期望,但这意味着,如果用户类型<代码>0.5<>>>>/代码>将被迫到<代码>0.50<<>>,则该解决办法取决于您的希望。

问题回答

我随函附上关于(变化)的活动,以了解你希望掌握的零 s。

$( .number-input ).on( change , function(){
    $(this).val(parseFloat($(this).val()).toFixed(2));
});

它只是从价值上看,将其投向浮标,将其推到小数点,并将其重新列为价值。





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签