繁体   English   中英

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

[英]how to get the maxlength of an input field using javascript

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

<input type="password" id="password" maxlength="20" >

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

console.log(document.getElementById("password").maxlength);

使用DOMElement::getAttribute()获取未在DOMElement中列出但在标记中存在的属性:

var el = document.getElementById("password");
console.log(el.getAttribute('maxlength'));
document.getElementById("password").maxLength

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

W3学校示例

接受的答案实际上是错误的:

document.getElementById("password").maxLength

将获得maxLength属性,例如maxLength="2" ,以获得您必须得到的值:

document.getElementById("password").maxLength.value /* <-- note .value */

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM