:input 选择器
分类:选择器 > 表单选择器 | 选择器 > jQuery扩展选择器
input selector
描述:选择所有的<input>输入框、<textarea>文本框、<select>下拉选择框以及<button>按钮元素。
加入于: 1.0
jQuery( ":input" ):input选择器基本选择了所有的表单控件。
其它说明
- 因为
:input是一个jQuery扩展,并不是CSS规范文档的一部分,使用:input查询,不能充分利用原生DOM的querySelectorAll()方法来提高性能。要想在使用:input选择元素时获得最佳性能,请先使用一个纯CSS选择器选择元素,然后使用.filter(":input")作筛选。
示例
找到所有的<input>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>input demo</title>
<style>
textarea {
height: 25px;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<form>
<input type="button" value="Input Button">
<input type="checkbox">
<input type="file">
<input type="hidden">
<input type="image">
<input type="password">
<input type="radio">
<input type="reset">
<input type="submit">
<input type="text">
<select>
<option>Option</option>
</select>
<textarea></textarea>
<button>Button</button>
</form>
<div id="messages"></div>
<script>
var allInputs = $( ":input" );
var formChildren = $( "form > *" );
$( "#messages" ).text( "Found " + allInputs.length + " inputs and the form has " +
formChildren.length + " children." );
$( "form" ).submit(function( event ) {
event.preventDefault();
});
</script>
</body>
</html>
演示结果