【问题标题】:HTML5 Input type "number" in FirefoxFirefox 中的 HTML5 输入类型“数字”
【发布时间】:2011-03-17 04:32:08
【问题描述】:

我正在开发一个只有少数人在内部使用的应用程序,所以我很乐意告诉他们只使用 Firefox 或 Chrome,这样我就可以使用 HTML5。

我正在研究一个非常简单的功能:

<style> 
input:invalid { background-color: red; }
div.box {
border-color:#000; 
border-width:thin; 
border-style:solid;
}
</style>     
<input type="number" name="id"> <small>(if the box becomes red, make sure you didn't put a space)</small> 

它在 Chrome 中效果很好:它会变成红色,并且不会让你提交,而无需我编写任何其他代码。

火狐……没那么多。它就像我有一个“文本”类型。

这是一个已知问题吗?解决方法?

谢谢

【问题讨论】:

  • 有趣的文章。但是,就像我说的那样,与我在同一个办公室的 5 个人都在使用它。所以如果红色不好,我很快就会知道。
  • @Adam:现在是第 17 版......似乎仍然没有实现。
  • @Mark 显示我所知道的...这是浏览器支持表:caniuse.com/#feat=input-number
  • @Mark 在 mozilla 的人们仍然在徘徊,没有实现这个...第一个问题是在 bugzilla in 2006 上提出的。 7年。没有!难以置信。

标签: firefox forms html


【解决方案1】:

我正在使用 firefox,我在开发输入类型号时遇到了同样的问题,键入字符和空格等... 无论如何,我在此示例中使用 angular 2,它几乎类似于 javascript,因此您可以在每种情况下使用此代码: 这是html:

<input class="form-control form-control-sm" id="qte" type="number"  min="1" max="30" step="1" [(ngModel)]="numberVoucher"
     (keypress)="FilterInput($event)" />

这里是FilterInput函数:

FilterInput(event: any) {
        let numberEntered = false;
        if ((event.which >= 48 && event.which <= 57) || (event.which >= 37 && event.which <= 40)) { //input number entered or one of the 4 directtion up, down, left and right
            //console.log('input number entered :' + event.which + ' ' + event.keyCode + ' ' + event.charCode);
            numberEntered = true;
        }
        else {
            //input command entered of delete, backspace or one of the 4 directtion up, down, left and right
            if ((event.keyCode >= 37 && event.keyCode <= 40) || event.keyCode == 46 || event.which == 8) {
                //console.log('input command entered :' + event.which + ' ' + event.keyCode + ' ' + event.charCode);
            }
            else {
                //console.log('input not number entered :' + event.which + ' ' + event.keyCode + ' ' + event.charCode);
                event.preventDefault();
            }
        }
        // input is not impty
        if (this.validForm) {
            // a number was typed
            if (numberEntered) {
                let newNumber = parseInt(this.numberVoucher + '' + String.fromCharCode(event.which));
                console.log('new number : ' + newNumber);
                // checking the condition of max value
                if ((newNumber <= 30 && newNumber >= 1) || Number.isNaN(newNumber)) {
                    console.log('valid number : ' + newNumber);
                }
                else {
                    console.log('max value will not be valid');
                    event.preventDefault();
                }
            }
            // command of delete or backspace was types
            if (event.keyCode == 46 || event.which == 8) {
                if (this.numberVoucher >= 1 && this.numberVoucher <= 9) {
                    console.log('min value will not be valid');
                    this.numberVoucher = 1;
                    //event.preventDefault();
                    this.validForm = true;
                }
            }
        }
        // input is empty
        else {
            console.log('this.validForm = true');
            this.validForm = false;
        }
    };

在这个函数中我只需要让数字、方向、删除的按键进入,所以这个函数只适用于正整数而不是加倍。

【讨论】:

    【解决方案2】:

    input type="number" 尚未在 Firefox 版本 25(2013 年 11 月)中实现。

    Bug 344616 是 Bugzilla@Mozilla 中的相关票证:https://bugzilla.mozilla.org/show_bug.cgi?id=344616

    2014 年 3 月 10 日更新 - 好消息!看起来该票已在 Firefox 29 中修复,scheduled 将于 2014 年 4 月 29 日发布。

    2014 年 4 月 30 日更新 - 确认,我刚刚试了一下,Firefox 29 支持 input type="number"

    【讨论】:

      【解决方案3】:

      Firefox 从 4.0 开始支持模式

       <input name="hours" type="number" pattern="[-+]?[0-9]*[.,]?[0-9]+"/>
      

      (如需帮助,请参阅http://html5pattern.com/

      【讨论】:

        【解决方案4】:

        这是一个已知问题吗?

        是的。未知类型被视为文本。 (而且只出现在草案规范中的类型往往被许多浏览器所未知)

        解决方法?

        JavaScript

        【讨论】:

          【解决方案5】:

          没有浏览器得到这个。里程可能会有所不同,但在所有浏览器中肯定有工作要做。

          Opera 会显示一个用户界面,让您单击上下箭头来增加或减少数字。 但是,即使输入了除数字以外的任何内容,您也可以提交表单。辅助功能还不完善,但至少您还可以使用键盘的箭头来增加和减少数字。

          Chrome 还没有该数字的 UI,因此没有帮助或 没有视觉提示需要一个数字。 Chrome 也没有真正的错误消息。将输入边框略微变红作为默认设置绝对不够好,而且它完全无法访问。在可访问性方面,Chrome 基本上是所有浏览器中最严重的违规者,例如对 ARIA 的零支持。

          您正在使用 :invalid 伪类 使整个输入小部件变为红色。请认识到这可能比 Chrome 中的默认样式更清晰,但它仍然不是一个可访问的解决方案。由于 Chrome 不支持 ARIA,坏消息是即使您通过 JavaScript 提供文本错误消息,盲人用户或其他任何使用屏幕阅读器的人也可能根本听不到。

          Firefox 可能迟到了,但请认识到 Mozilla 拥有very strict shipping criteria for its features,而 Chrome 却不考虑后果。

          【讨论】:

            【解决方案6】:

            首先,您使用的是 Firefox 4 吗? HTML5 表单在版本 4 中得到了更好的支持。

            此页面包含有关 HTML5 表单和当前错误的详细信息 https://wiki.mozilla.org/User:Mounir.lamouri/HTML5_Forms

            更新:如果浏览器不支持您要使用的 HTML5 功能,请尝试Modernizr。它使用 Javascript 来增强支持。它的文档有information about input types

            【讨论】:

            • 我宁愿等待发布版本,然后再进行升级。 :( 我虽然 3.6 就足够了 ...
            • 没关系。 Firefox 4 beta 尚不支持输入数字。不过,它可能会出现在最终版本中。
            • Firefox 12 (Linux) 仍然不支持输入数字。真可惜。 Modernizr 看起来不错。
            • 我在最新的 Firefox nightly (Firefox 17.0a1 (2012-08-01)) 中仍然看不到对这种类型的支持。我怀疑它会很快到来。
            • 这很奇怪。有谁知道为什么 Mozilla 拒绝支持它?
            猜你喜欢
            • 1970-01-01
            • 2013-01-04
            • 1970-01-01
            • 2015-09-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-07-20
            • 2016-02-18
            相关资源
            最近更新 更多