输入类型=“数字"时忽略maxlength在 Chrome 中 [英] maxlength ignored for input type="number" in Chrome

查看:19
本文介绍了输入类型=“数字"时忽略maxlength在 Chrome 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

maxlength 属性不适用于 .这只发生在 Chrome 中.

The maxlength attribute is not working with <input type="number">. This happens only in Chrome.

<input type="number" class="test_css"  maxlength="4"  id="flight_number" name="number"/>

推荐答案

来自

如果type属性的值为textemailsearchpasswordtelurl,该属性指定用户可以输入的最大字符数(以 Unicode 代码点为单位);对于其他控件类型,它被忽略.

If the value of the type attribute is text, email, search, password, tel, or url, this attribute specifies the maximum number of characters (in Unicode code points) that the user can enter; for other control types, it is ignored.

因此 maxlength 上被设计忽略了.

So maxlength is ignored on <input type="number"> by design.

根据您的需要,您可以使用 minmax 属性作为 inon 在他/她的回答中建议的(注意:这将仅定义受限范围,而不是值的实际字符长度,尽管 -9999 到 9999 将涵盖所有 0-4 位数字),或者您可以使用常规文本输入并使用新的 pattern 属性:

Depending on your needs, you can use the min and max attributes as inon suggested in his/her answer (NB: this will only define a constrained range, not the actual character length of the value, though -9999 to 9999 will cover all 0-4 digit numbers), or you can use a regular text input and enforce validation on the field with the new pattern attribute:

<input type="text" pattern="d*" maxlength="4">

这篇关于输入类型=“数字"时忽略maxlength在 Chrome 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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