maxlength属性对<input type="number">不起作用。这只发生在Chrome。
<input type="number" class="test_css" maxlength="4" id="flight_number" name="number"/>
maxlength属性对<input type="number">不起作用。这只发生在Chrome。
<input type="number" class="test_css" maxlength="4" id="flight_number" name="number"/>
当前回答
许多人张贴的onKeyDown()事件根本不工作,即你不能删除一旦你达到限制。因此,使用onKeyPress()而不是onKeyDown(),它工作得很好。
下面是工作代码:
用户将不允许输入超过4位数字 < br > <input type="number" pattern="/^-?\d+\.? "\d*$/" onKeyPress="if(this.value.length==4) return false;"/>
其他回答
对于React用户,
只需将10替换为您的最大长度要求
<input type="number" onInput={(e) => e.target.value = e.target.value.slice(0, 10)}/>
最大长度将不能与<input type="number"工作,我知道的最好的方法是使用oninput事件限制最大长度。请看下面的代码。
<input name="somename"
oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);"
type = "number"
maxlength = "6"
/>
您可以使用min和max属性。
下面的代码做同样的事情:
<输入类型=“数字” 最小=“-999” 最大=“9999”/>
我会让这个快速和容易理解!
使用min= "和max= "代替maxlength for type='number' (maxlength用于定义文本类型中字符串的最大字母数)。
干杯
在React上试试吧。
<input
onInput={(e) => {
if (e.target.value.length > e.target.maxLength)
e.target.value = e.target.value.slice(0,e.target.maxLength);
}}
type = "number"
maxlength = {6}
/>