jquery获取表单元素
2011-06-14 18:15
225 查看
表单验证需要获得表单元素,下面是获取表单元素的方法
例子:
View Code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script src="js/jquery-1.6.min.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(function($){
$("#btnCheck").click(function(){
$("#msn").html(
"input元素有:"+$(":input").length+"个(取得所有表单元素)<br/>"+
"text元素有:"+$(":text").length+"个(取得所有表单元素)<br/>"+
"password元素有:"+$(":password").length+"个(取得所有表单元素)<br/>"+
"radio元素有:"+$(":radio").length+"个(取得所有表单元素)<br/>"+
"checkbox元素有:"+$(":checkbox").length+"个(取得所有表单元素)<br/>"+
"button元素有:"+$(":button").length+"个(取得所有表单元素)<br/>"
);
})
})
</script>
</head>
<body>
<form id="form1" runat="server">
<label>用户名:</label><input type="text" id="userName" /><br />
<label>密 码:</label><input type="password" id="userPwd" /><br />
<label>性 别:</label><input type="radio" name="sex" />男
<input type="radio" name="sex" />女<br />
<label>爱好:</label><input type="checkbox" name="chk" />上网
<input type="checkbox" name="chk" />唱歌<br />
<input id="btnCheck" type="button" value="button" />
<span id="msn"></span>
</form>
</body>
</html>
:input:所有表单元素
:text:获得所有text元素的元素,如要获得所有值,必须有each循环获得
:password:获得属性为password的元素
:checkbox:获得所有checkbox元素的元素,如要获得选中的值,则需要用each循环获得(:checkbox:checked)
等等....
例子:
View Code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script src="js/jquery-1.6.min.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(function($){
$("#btnCheck").click(function(){
$("#msn").html(
"input元素有:"+$(":input").length+"个(取得所有表单元素)<br/>"+
"text元素有:"+$(":text").length+"个(取得所有表单元素)<br/>"+
"password元素有:"+$(":password").length+"个(取得所有表单元素)<br/>"+
"radio元素有:"+$(":radio").length+"个(取得所有表单元素)<br/>"+
"checkbox元素有:"+$(":checkbox").length+"个(取得所有表单元素)<br/>"+
"button元素有:"+$(":button").length+"个(取得所有表单元素)<br/>"
);
})
})
</script>
</head>
<body>
<form id="form1" runat="server">
<label>用户名:</label><input type="text" id="userName" /><br />
<label>密 码:</label><input type="password" id="userPwd" /><br />
<label>性 别:</label><input type="radio" name="sex" />男
<input type="radio" name="sex" />女<br />
<label>爱好:</label><input type="checkbox" name="chk" />上网
<input type="checkbox" name="chk" />唱歌<br />
<input id="btnCheck" type="button" value="button" />
<span id="msn"></span>
</form>
</body>
</html>
:input:所有表单元素
:text:获得所有text元素的元素,如要获得所有值,必须有each循环获得
:password:获得属性为password的元素
:checkbox:获得所有checkbox元素的元素,如要获得选中的值,则需要用each循环获得(:checkbox:checked)
等等....
相关文章推荐
- jQuery 获取、设置表单元素的值
- jQuery 练习[二]: 获取对象(3) - 根据属性、内容匹配, 还有表单元素匹配
- jquery获取form表单中元素
- Jquery给easyui的表单元素赋值,获取值总结
- jQuery获取form表单元素的值
- jQuery获取,遍历和操作表单元素Select,checkbox,radio
- jquery获取表单元素select option的值,对select option操作
- jQuery 获取对象 根据属性、内容匹配, 还有表单元素匹配
- jquery中通过过滤器获取表单元素的实现代码
- jquery获取表单元素
- jquery获取元素值的方法(常见的表单元素)
- jquery 循环获取表单下一个元素(回车绑定tab事件)
- 关于jquery获取表单中一下简单的元素的值
- jQuery name属性与checked属性结合获取表单元素值
- jquery获取form表单中元素
- jQuery获取表单各元素的值
- jQuery 获取表单元素中值的方法
- jQuery 获取和设置表单元素
- jQuery获取表单各元素的值
- jQuery获取Select选择的Text(非表单元素)和 Value(表单元素)(转)