日期:2014-05-16 浏览次数:20403 次
<form> 性别 <input type="radio" name="sex"> 男 <input type="radio" name="sex"> 女 爱好 <input type="radio" name="enjoy">看书 <input type="radio" name="enjoy">逛街 <input type="radio" name="enjoy">运动 政治面貌 <input type="radio" name="zhengzhi">团员 <input type="radio" name="zhengzhi">党员 <input type="submit" value="提交"> </form>
function toSubmit() { var s=true; $(":radio").each(function(){ var radio = $(this).attr("name"); if($(":radio[name='"+radio+"']").attr("checked")==false) { s=false; } }) if(s==false) { alert("所有项为必填!"); } return s;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>无标题页</title> <script type="text/javascript" src="../script/jquery-1.7.js"></script> <script type="text/javascript"> window.onload=function(){ $("#b1").click(function(){ alert($(":radio:checked").length); }); }; </script></head> <body> <input type="radio" name="sex"> 男 <input type="radio" name="sex"> 女 爱好 <input type="radio" name="enjoy">看书 <input type="radio" name="enjoy">逛街 <input type="radio" name="enjoy">运动 政治面貌 <input type="radio" name="zhengzhi">团员 <input type="radio" name="zhengzhi">党员 <input type="button" id="b1" value="click me" /> </body> </html>
------解决方案--------------------
<!doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Test</title> <script src="jquery-1.4.2.js"></script> </head> <body> <form> 性别: <input type="radio" name="sex"> 男 <input type="radio" name="sex"> 女<br /> 爱好: <input type="radio" name="enjoy">看书 <input type="radio" name="enjoy">逛街 <input type="radio" name="enjoy">运动<br /> 政治面貌: <input type="radio" name="zhengzhi">团员 <input type="radio" name="zhengzhi">党员<br /> <input type="submit" value="提交"> </form> <script> function toSubmit() { var count = 0; $("input[type=radio]:checked").each(function () { count++; }); if (count != 3) { alert("所有项为必填!"); } } $(":submit").click(toSubmit); &