表單驗證需要獲得表單元素,下面是獲取表單元素的方法
例子:
<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)
原帖:http://www.cnblogs.com/shuang121/archive/2011/06/14/2080876.html