如何使用javascript获取输入字段的最大长度

Tec*_*ech 3 html javascript maxlength

我只是想知道我们是否可以从javascript获取输入字段的最大长度

<input type="password" id="password" maxlength="20" >
Run Code Online (Sandbox Code Playgroud)

我尝试了这个,但它返回undefined

console.log(document.getElementById("password").maxlength);
Run Code Online (Sandbox Code Playgroud)

Bli*_*itZ 9

使用DOMElement::getAttribute()获得的属性,在一个DOMElement没有上市,但现有的标记:

var el = document.getElementById("password");
console.log(el.getAttribute('maxlength'));
Run Code Online (Sandbox Code Playgroud)


Tyl*_*ler 8

document.getElementById("password").maxLength
Run Code Online (Sandbox Code Playgroud)

要使用Javascript访问它,您需要一个大写的'L'

W3学校示例

  • 在这里链接到W3学校是危险的,它可能会让你失去选票以获得其他好的答案.请参阅http://www.w3fools.com/,了解原因. (6认同)