[英]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
将获得maxLength
属性,例如maxLength="2"
,以获得您必须得到的值:
document.getElementById("password").maxLength.value /* <-- note .value */
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.