【问题标题】:Force chrome not to convert dot to comma in input type = number强制chrome不要在输入类型=数字中将点转换为逗号
【发布时间】:2016-03-14 06:46:52
【问题描述】:

我有一个角度应用程序,其中我使用了 input type = "number",其中我从服务中获取值。该服务以小数点形式为我提供这些数字,例如:0.5、0.6、0.7。 所以我的文本框显示 0.5, 0.6 , 0.7 但是当我将浏览器语言更改为法语时,文本框显示 0,5,因为法语中使用逗号表示小数。

<input type="number" value=0.5>

Output:French language: 0,5
       English language: 0.5

我不希望这种点到逗号的转换。 angularjs或javascript中有什么方法可以阻止浏览器将点转换为逗号? 我不想更改输入的类型,因为有很多验证。

【问题讨论】:

    标签: javascript angularjs html google-chrome


    【解决方案1】:

    您可以在 HTML 标记中添加 lang 属性,如下所示:

    <html lang="en">
    

    【讨论】:

    • 我已经添加了这个。但是点到逗号的转换仍然发生。
    【解决方案2】:

    尝试以下指令。如果它解决了你的问题。

    directive('smartFloat', function() {
         var FLOAT_REGEXP = /^\-?\d+((\.|\,)\d+)?$/;
         return {
             require: 'ngModel',
             link: function(scope, elm, attrs, ctrl) {
                 ctrl.$parsers.unshift(function(viewValue) {
                     if (FLOAT_REGEXP.test(viewValue)) {
                         ctrl.$setValidity('float', true);
                         if(typeof viewValue === "number")
                             return viewValue;
                         else
                             return parseFloat(viewValue.replace(',', '.'));
                     } else {
                        ctrl.$setValidity('float', false);
                        return undefined;
                     }
                });
            }
        };
    });
    

    并按如下方式使用:

    <input type="text" smartFloat value="0.5" />
    

    我从这个链接中找到了这个:http://www.wmyl.se/en/blog/comma-dot-input-typenumber-mess/

    【讨论】:

    • html 标签已经有 lang="en"。但我仍然尝试了上述方法。转换仍然发生。
    • 没有requirement 让客户注意到 lang 属性:“…lang 属性可能由用户代理使用……”(我的重点)。
    • 是的,你是对的,它不适用于 lang="en"。虽然我没有尝试过这个答案,但更新了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    • 2020-11-11
    相关资源
    最近更新 更多