在js里做入力项目长度检查,请问用jquery怎么获取文本框的Maxlength 。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<input type="text" maxlength="200" id="demo">
<id id="txt"></id>
</body>
<script>
let txt = document.getElementById("demo");
//js写法
js_text = txt.getAttribute("maxlength")
// jquery 写法
jquery_text = $("#demo").attr('maxlength');
$("#txt").html("<br />js_text:" + js_text + "<br />");
$("#txt").append("jquery_text:" + jquery_text + "<br/>");
</script>
</html>
设置了的话 获取 dom.maxLength 试试
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text" maxlength="200" id="txt">
</body>
<script>
let txt = document.getElementById("txt");
console.log(txt.maxLength)
// jquery 写法
// $("#text").maxLength
</script>
</html>
Maxlength是属性,使用jquery获取属性值:var shuxingzhi = $("#id").attr("Maxlength");可以用去获取任何属性值。