Chrome自动格式输入=数字 [英] Chrome auto formats input=number

查看:147
本文介绍了Chrome自动格式输入=数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个web应用程序,我在其中指定一个输入字段,使用HTML5属性type =number。

 < input type =numbervalue =123456/> 

通过指定类型,Chrome会自动格式化该值以包含逗号(123,456)。在其他浏览器中,它不会格式化数字,但它也不会阻止非数字字符。

在这种情况下,我不希望添加逗号。有什么方法可以关闭本地化的格式化?

解决方案

这是因为与HTML5 数字在Chromium中输入类型,而您绝对是不是唯一的一个不关心这一点。



我以前通过使用文本来解决这个问题类型。例如,这已经运行良好(刚刚在Chrome 11.0.696.71中测试过):

 < ; input type =text
placeholder =输入文本
name =inputName
pattern =[0-9] *>

数字类型的行为我,至少)绝对是一个错误,因为 HTML5标准指定编号在格式化为显示时应具有以下值:


在给定数字输入的情况下,将数字转换为字符串的算法如下所示:返回表示输入的有效浮点数。


标准定义了一个有效浮点数的数字这里,并且据我所知,包括分组字符在内并不是预期的。






更新



我把这个问题隔离在下面的代码中WebKit的。我在这里也包括了解决问题的线路:


$ b

  //来自LocalizedNumberICU。 cpp 
String formatLocalizedNumber(double number,unsigned fractionDigits)
{
NumberFormat * formatter = numberFormatter();
if(!formatter)
return String();
UnicodeString结果;
formatter-> setMaximumFractionDigits(clampToInteger(fractionDigits));
formatter-> setGroupingUsed(FALSE); //添加此行以修复问题
formatter->格式(数字,结果);
return String(result.getBuffer(),result.length());
}

我在下周休假,但计划将此补丁提交给WebKit团队,当我回来。一旦他们(希望)接受该补丁,Chromium应该将其作为其正常刷新过程的一部分。





您可以在这里看到原始代码,已修补的修订此处以及
原始文件和修补文件这里。最后的补丁是由Shinya Kawanaka创建的。



I have a web application where I'm specifying an input field to be a number using the HTML5 property type="number".

<input type="number" value="123456" />

By specifying the type, Chrome automatically formats the value to include a comma (123,456). In other browsers it does not format the number, but it also does not prevent non-numeric characters.

In this case, I don't want the comma to be added. Is there any way to turn off localized formatting?

解决方案

This is occurring because of the behavior associated with the HTML5 number input type in Chromium, and you are definitely not the only one that doesn't care for this.

I have worked around this issue in the past by using the text type. For example, this has worked well (tested just now in Chrome 11.0.696.71):

<input type="text" 
       placeholder="Enter Text" 
       name="inputName" 
       pattern="[0-9]*">

This behavior of the number type (to me, at least) is definitely a bug, because the HTML5 standard specifies the number should have the following value when formatted for display:

The algorithm to convert a number to a string, given a number input, is as follows: Return a valid floating point number that represents input.

And the standard defines a "valid floating point" number here, and as far as I can see, including grouping characters is not expected.


Update

I've isolated the issue to the following code down in the guts of WebKit. I've included the line that fixes the issue here as well:

// From LocalizedNumberICU.cpp
String formatLocalizedNumber(double number, unsigned fractionDigits)
{
    NumberFormat* formatter = numberFormatter();
    if (!formatter)
        return String();
    UnicodeString result;
    formatter->setMaximumFractionDigits(clampToInteger(fractionDigits));
    formatter->setGroupingUsed(FALSE); // added this line to fix the problem
    formatter->format(number, result);
    return String(result.getBuffer(), result.length());
}

I'm on vacation next week, but plan on submitting this patch to the WebKit team when I return. Once they (hopefully) accept the patch, Chromium should pull it in as part of its normal refresh process.


You can see the original code here, the patched revision here, and the diff of the original file and the patched file here. The final patch was created by Shinya Kawanaka.

这篇关于Chrome自动格式输入=数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆