4. 表单对象过滤选择器
在开始之前,先介绍两个属性disabled属性和multiple属性,它们和checked属性、selected属性一样:
checked=“checked” 表示多选框或单选框中的被初始时选中,不写就不选中。
selected=“selected” 表示下拉列表中的option项是否被选中
disabled=“disabled” 表示input输入框是否可用,默认可用,为enabled,disabled时不可用
multiple=“multiple” 表示下拉列表是否允许多选,默认不允许多选,当multiple属性被赋值后允许多选。
可用元素: <input name="add" value="可用文本框1"/><br>
不可用元素: <input name="email" disabled="disabled" value="不可用文本框1"/><br>
可用元素: <input name="che" value="可用文本框2"/><br>
不可用元素: <input name="name" disabled="disabled" value="不可用文本框2"/><br>
下拉列表1: <br>
<select name="test" multiple="multiple" style="height: 100px" id="sele1">
<option>浙江</option>
<option selected="selected">辽宁</option>
<option>北京</option>
<option selected="selected">天津</option>
<option>广州</option>
<option>湖北</option>
</select>
表单过滤器
- :input 匹配所有 input, textarea, select 和 button 元素
- :text 匹配所有 文本输入框
- :password 匹配所有的密码输入框
- :radio 匹配所有的单选框
- :checkbox 匹配所有的复选框
- :submit 匹配所有提交按钮
- :image 匹配所有 img 标签
- :reset 匹配所有重置按钮
- :button 匹配所有 input type=button 按钮
- :file 匹配所有 input type=file 文件上传
- :hidden 匹配所有不可见元素 display:none 或 input type=hidden
表单对象属性过滤器:
- :enabled 匹配所有可用元素
- :disabled 匹配所有不可用元素
- :checked 匹配所有选中的单选,复选以及下拉列表中选中的 option 标签对象
- :selected 匹配所有选中的 下拉列表中选中的 option 标签对象
注意: 3:checked 过滤得到的元素是要 包含4:selected 的,可以对3的全集加以限制,只得到选中的单选、复选框的内容。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery-2021-01-30</title>
<script type="text/javascript" src="../script/jquery-1.7.2.js"></script>
<script type="text/javascript">
//1.对表单内 可用input 赋值操作
//2.对表单内 不可用input 赋值操作
//3.获取多选框选中的个数 使用size()方法获取选取到的元素集合的元素个数
//4.获取多选框,每个选中的value值
//5.获取下拉框选中的内容
</script>
</head>
<body>
<h3>表单对象属性过滤选择器</h3>
<button id="btn1">对表单内 可用input 赋值操作.</button>
<button id="btn2">对表单内 不可用input 赋值操作.</button><br /><br />
<button id="btn3">获取多选框选中的个数.</button>
<button id="btn4">获取多选框选中的内容.</button><br /><br />
<button id="btn5">获取下拉框选中的内容.</button><br /><br />
<form id="form1" action="#">
可用元素: <input name="add" value="可用文本框1"/><br>
不可用元素: <input name="email" disabled="disabled" value="不可用文本框1"/><br>
可用元素: <input name="che" value="可用文本框2"/><br>
不可用元素: <input name="name" disabled="disabled" value="不可用文本框2"/><br>
<br>
多选框: <br>
<input type="checkbox" name="newsletter" checked="checked" value="test1" />test1
<input type="checkbox" name="newsletter" value="test2" />test2
<input type="checkbox" name="newsletter" value="test3" />test3
<input type="checkbox" name="newsletter" checked="checked" value="test4" />test4
<input type="checkbox" name="newsletter" value="test5" />test5
<br><br>
下拉列表1: <br>
<select name="test" multiple="multiple" style="height: 100px" id="sele1">
<option>浙江</option>
<option selected="selected">辽宁</option>
<option>北京</option>
<option selected="selected">天津</option>
<option>广州</option>
<option>湖北</option>
</select>
<br><br>
下拉列表2: <br>
<select name="test2">
<option>浙江</option>
<option>辽宁</option>
<option selected="selected">北京</option>
<option>天津</option>
<option>广州</option>
<option>湖北</option>
</select>
</form>
</body>
</html>
界面效果如图所示:
功能实现:分别给5个按钮都绑定上相应的单击事件
<script type="text/javascript" src="../script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function () {
//1.对表单内 可用input 赋值操作
$("#btn1").click(function () {
$(":text:enabled").val("我是陆亿行-2021-01-31");
}); //jQuery对象
// val()可以操作表单项的value属性值(对jQuery对象而言),相当于dom对象的value
// 它可以进行设置和获取
//2.对表单内 不可用input 赋值操作
$("#btn2").click(function () {
$(":text:disabled").val("我不是陆亿行-2021-01-31");
});
//3.获取多选框选中的个数 使用size()方法获取选取到的元素集合的元素个数
$("#btn3").click(function () {
alert('多选框的个数:'+$(":checkbox").length);
alert('选中的个数:'+$(":checkbox:checked").length);
});
//4.只获取多选框,每个选中的value值
$("#btn4").click(function () {
//var $checkboxs=$(":checkbox:checked");
var $checkboxs=$("input:checked");
//都可,前面必须要加以限制,否则只用checked还会获得下拉列表select里选中的内容
//法一:老式遍历
for (var i=0;i<$checkboxs.length;i++){
alert( $checkboxs[i].value );
}
//Dom对象
//法二:each方法是jQuery对象提供用来遍历元素的方法
$checkboxs.each(function () {
alert( this.value );
});//在遍历的function函数中,有一个this对象,这个this对象,就是当前遍历到的dom对象
});
//5.获取下拉框选中的内容
$("#btn5").click(function () {
var $options=$(":selected");
// var $options = $("select option:selected"); 一样
$options.each(function () {
//alert( this.value );
alert( this.innerHTML );
}); //二者效果是一样的
});
});
</script>