jquery表单对象属性过滤选择器实例分析
本文实例讲述了jquery表单对象属性过滤选择器用法。分享给大家供大家参考。具体分析如下:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>表单对象属性过滤选择器</title>
<scriptsrc="jquery-1.6.2.min.js"type="text/javascript"></script>
<scripttype="text/javascript">
$(function(){
//=========举例1===========================
//:enabled用法:$(”input:enabled”)返回值集合元素
//说明:匹配所有可用元素.意思是查找所有input中不带有disabled=”disabled”的input.不为disabled,
//当然就为enabled啦.
$("input:enabled").val("我是有效的按钮");
//=========举例2===========================
//:disabled用法:$(”input:disabled”)返回值集合元素
//说明:匹配所有不可用元素.与上面的那个是相对应的.
$("input:disabled").val("我是无效的按钮");
//=========举例3===========================
//:checked用法:$(”input:checked”)返回值集合元素
//说明:匹配所有选中的被选中元素(复选框、单选框等,不包括select中的option).这话说起来有些绕口.
//...<1>提取所有选中的name='love'的复选框
$("#btnTest").click(function(){
ShowElements($("input[name='love']:checked"));//第一种写法
//ShowElements($("[name='love']:checked"))//第二种写法
});
//=========举例4===========================
//:selected用法:$(”selectoption:selected”)返回值集合元素
//说明:匹配所有选中的option元素.
//...<1>所有name='city'的下拉框的选中项
$("#btnTest2").click(function(){
ShowElements($("select[name='city']option:selected"));
});
//...<2>所有name='prov'的下拉框的选中项
//$("#btnTest2").click(function(){
//ShowElements($("select[name='prov']option:selected"));
//});
});
functionShowElements(arr){
//alert(arr.length);
varoutput="";
for(vari=0;i<arr.length;i++){
if(output==""){
output=arr.eq(i).val();
}
else{
output+=","+arr.eq(i).val();
}
}
alert(output);
}
</script>
</head>
<body>
<inputtype="button"disabled="disabled"value="button1"/>
<inputtype="button"value="button2"/>
<inputtype="button"value="button3"/>
<inputtype="button"disabled="disabled"value="button4"/>
<inputtype="button"id="btnTest"value="点击我检查复选框"/>
<inputtype="checkbox"name="love"value="1"/>足球
<inputtype="checkbox"name="love"value="2"/>篮球
<inputtype="checkbox"name="love"value="3"/>排球
<inputtype="checkbox"name="Other"value="3"/>非爱好类<br/>
我是下拉框<inputtype="button"id="btnTest2"value="点击我检查下拉框"/>
<selectname="city">
<optionvalue='beijing'>北京</option>
<optionvalue='shanghai'>上海</option>
<optionvalue='shenzhen'>深圳</option>
</select>
<selectname="prov">
<optionvalue='jiangxi'>江西省</option>
<optionvalue='shichuan'>四川省</option>
<optionvalue='guangdong'>广东省</option>
</select>
</body>
</html>
更多关于jquery选择器相关内容感兴趣的读者可查看本站专题:《jquery选择器用法总结》
希望本文所述对大家的jQuery程序设计有所帮助。