如何用jQuery找到所有启用的输入元素
jQuery [":enabled"]
伪类选择器有助于找到所有enabled的输入元素。这个伪类选择器应该只用于选择支持disabled属性的HTML元素,即 (<button>, <input>, <textarea>, <select>, <option>, <optgroup>
)
与其他伪类选择器一样,如[(a:hover)
),也建议在它前面加上一个标签名或其他选择器, 否则,通用选择器(”*
“)将被暗示为[$( "*:enabled" )
)或$( “input:enabled”)。
语法:
$( "input:enabled" ).val( "This box is enabled" );
注意: (:enabled)选择的元素,其boolean_disabled属性严格地为false。
HTML代码:下面的代码演示了:enabled伪类选择器,以找到所有使用jQuery的输入元素。
<!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>Elements that are enabled</title>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
<h1 style="color: green; margin-left: 120px;">GeeksforGeeks</h1>
<!-- The form contains three boxes out
of which two boxes are disabled -->
<form>
<!-- Set input disabled -->
<input style="border: 0.8px solid green;" name="email" disabled>
<input style="border: 0.8px solid green;" name="id">
<input style="border: 0.8px solid green;" name="email" disabled>
</form>
<!-- The val is displayed in the box in
which the input element is enabled -->
<script>
$( "input:enabled" ).val( "This box is enabled" );
</script>
</body>
</html>
输出: