专门用于选取表单元素的选择器:
$("#form2:enabled"):选取id为form2表单内所有启用元素$("#form2:disabled"):选取id为form2表单内所有禁用元素$("input:checked"):选取所有选中的单选框、复选框$("select option:selected"):选取下拉框选中的选项$(":input"):选取<input>、<textarea>、<select>、<button>全部表单元素;和$("input")不同,$("input")只匹配input标签$(":text"):单行文本框,等价$("input[type=text]")$(":password"):密码框;同类还有:radio、:checkbox、:submit、:image、:reset、:button、:file、:hidden
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm18.aspx.cs" Inherits="WebApplication1.WebForm18" %>
<!DOCTYPE html>
<html xmlns="[http://www.w3.org/1999/xhtml](http://www.w3.org/1999/xhtml)">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="[http://libs.baidu.com/jquery/1.8.3/jquery.min.js](http://libs.baidu.com/jquery/1.8.3/jquery.min.js)"></script>
<style>
</style>
<script type="text/javascript">
function deal()
{
alert($('input').length);
//将所有选中的表单元素 设置背景颜色
$('input:checked').css("background-color", 'green');
//获取表单中checkbox元素 并且被勾选的
alert($('input[type=checkbox]:checked').val());
//获取所有的文本框
alert($('input[type=text]').length);
//获取所有的radio
alert($(':radio').length);
//获取禁用的文本框
alert($('input[type=text]:disabled').val());
//获取所有的禁用的元素
alert($('input:disabled').length);
}
</script>
</head>
<body>
<input type="text" />
<input type="text" disabled="disabled" />
<input type="radio" name="sport" value="football" />
<input type="radio" name="sport" value="basketball" disabled="disabled" />
<input type="checkbox" value="foxdevelop" />
<input type="checkbox" />
<input type="button" onclick="deal()" value="执行" />
</body>
</html>
Code language: HTML, XML (xml)
说明:点击执行按钮,依次弹出各类表单元素数量、取值;勾选的input会设置绿色背景。
Previous: 属性过滤选择器
Next: 练习一下-左右勾选框的全选 反选的功能