:radio 选择器
分类:选择器 > 表单选择器 | 选择器 > jQuery扩展选择器
radio selector
描述:选择所有的type="radio"的<input>元素。
加入于: 1.0
jQuery( ":radio" )$( ":radio" )等同于$( "[type=radio]" )。与别的伪类选择器一样(它们以一个“:”开头),建议在它前面用一个标签名或者别的选择器,否则,会潜在地使用普遍选择器("*")。换句话说,光裸的$( ":radio" )等同于$( "*:radio" ),所以应该使用$( "input:radio" )来代替它。
要想选择一个互相关联的单选钮,你可以使用$( "input[name=gender]:radio" )。
其它说明
- 因为
:radio是一个jQuery扩展,并不是CSS规范文档的一部分,使用:radio查询,不能充分利用原生DOM的querySelectorAll()方法来提高性能。要想在现代浏览器中获得更好的性能,请使用[type="radio"]来代替它。
示例
查找所有的type="radio"的<input>。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>radio 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" name="asdf">
<input type="radio" name="asdf">
<input type="reset">
<input type="submit">
<input type="text">
<select>
<option>Option</option>
</select>
<textarea></textarea>
<button>Button</button>
</form>
<div></div>
<script>
var input = $( "form input:radio" )
.wrap( "<span></span>" )
.parent()
.css({
background: "yellow",
border: "3px red solid"
});
$( "div" )
.text( "For this type jQuery found " + input.length + "." )
.css( "color", "red" );
// Prevent form submission
$( "form" ).submit(function( event ) {
event.preventDefault();
});
</script>
</body>
</html>
演示结果