我使用 <input type="text" name="username">
创建了一个输入框,供用户输入 username
,然后我将长度固定为 5 进行一些计算。
<input type="text" name="username" maxlength="5">
。但是当我将输入作为静态文本给出时,它将接受任意数量的字符。
将最大长度设置为 5 意味着输入框只能容纳 5 个字母,那么为什么这样输入时会容纳超过 5 个字母呢?
ojit_代码
最佳答案
根据w3schools ,
Maxlength eflects the maxlength HTML attribute, containing the maximum length of text (in Unicode code points) that the value can be changed to. The constraint is evaluated only when the value is changed
这里的关键是它仅在值更改时进行评估。您可以在创建元素时将该值设置为任何值。更改后就会出现效果。尝试更改示例中的输入,您会发现它确实将其限制为 5 个字符。
关于html - 静态输入违反输入框的 MaxLength 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25418420/