jQuery表單選擇器
無論是提交或傳遞數據,表單元素在動態互動頁面的作用是非常重要的。 jQuery中專門加入了表單選擇器,因此能夠極為方便地取得到某個類型的表單元素
#表單選擇器的具體方法描述:

注意事項:
除了input篩選選擇器,幾乎每個表單類別篩選器都對應一個input元素的type值。大部分錶單類別篩選器可以使用屬性篩選器替換。例如$(':password') == $('[type=password]')
如果想得到表單內表單元素的個數,程式碼如下:
$("#form1 :input").length; //注意与$("#form1 input")的区别如果想得到表單內單行文字方塊的數,程式碼如下:
$("#form1 :text").length;如果想得到表單內密碼方塊的數,程式碼如下:
$("#form1 :password").length;同理,其他表單選擇器的操作與此類似
取得頁面中的所有<p>元素,給每一個<p>元素新增onclick事件,例如:
$("p").click(function({
//doing somethingr(操作)
})取得id為tb的元素,然後尋找它下面的tbody標籤,再尋找tbody下索引值是偶數的tr元素改變它的背景色(css("property","value");用來設定jQuery物件的樣式)例如:
$('#tb tbody tr:even').css("backgroundColor","#888");
先使用屬性選擇器,然後用表單物件屬性過濾,最後取得jQuery物件的長度,例如:
$('#btn').click(function(){
var length=$("input[name='check']:checked").length;
alert("选中的个数为:"+length);
})清空所有input type="text" 文字方塊內容:$("input:text").val("");
新建檔案
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
input{
display: block;
margin: 10px;
padding:10px;
}
</style>
<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
<style>
input{
display: block;
margin: 10px;
padding:10px;
}
</style>
</head>
<body>
<h2>子元素筛选选择器</h2>
<h3>input、text、password、radio、checkbox</h3>
<h3>submit、image、reset、button、file</h3>
<div class="left first-div">
<form>
<input type="text" value="text类型"/>
<input type="password" value="password"/>
<input type="radio"/>
<input type="checkbox"/>
<input type="submit" />
<input type="image" />
<input type="reset" />
<input type="button" value="Button" />
<input type="file" />
</form>
</div>
<script type="text/javascript">
//查找所有 input, textarea, select 和 button 元素
//:input 选择器基本上选择所有表单控件
$(':input').css("border", "1px groove red");
</script>
<script type="text/javascript">
//匹配所有input元素中类型为text的input元素
$('input:text').css("background", "#A2CD5A");
</script>
<script type="text/javascript">
//匹配所有input元素中类型为password的input元素
$('input:password').css("background", "yellow");
</script>
<script type="text/javascript">
//匹配所有input元素中的单选按钮,并选中
$('input:radio').attr('checked','true');
</script>
<script type="text/javascript">
//匹配所有input元素中的复选按钮,并选中
$('input:checkbox').attr('checked','true');
</script>
<script type="text/javascript">
//匹配所有input元素中的提交的按钮,修改背景颜色
$('input:submit').css("background", "#C6E2FF");
</script>
<script type="text/javascript">
//匹配所有input元素中的图像类型的元素,修改背景颜色
$('input:image').css("background", "#F4A460");
</script>
<script type="text/javascript">
//匹配所有input元素中类型为按钮的元素
$('input:button').css("background", "red");
</script>
<script type="text/javascript">
//匹配所有input元素中类型为file的元素
$('input:file').css("background", "#CD1076");
</script>
</body>
</html>
預覽
Clear
- 課程推薦
- 課件下載
課件暫不提供下載,工作人員正在整理中,後期請多關注該課程~ 















