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

问题描述 投票:3回答:3

我只是想知道,如果我们能得到从JavaScript输入字段的最大长度

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

我试过,但它返回undefined

console.log(document.getElementById("password").maxlength);
javascript html maxlength
3个回答
8
投票
document.getElementById("password").maxLength

使用JavaScript访问它,你需要一个大写字母“L”

W3 Schools Example


9
投票

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

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

0
投票

该接受的答案其实是错误的:

document.getElementById("password").maxLength

将让你的maxLength属性如maxLength="2",让你必须得到公正的价值:

document.getElementById("password").maxLength.value /* <-- note .value */
© www.soinside.com 2019 - 2024. All rights reserved.