|
在一个表单中,有个单选按钮组,本文以男女单选按钮组示例讲解,在客户端验证用户是否对每个选项进行了选择。如果没有选择,则给出相应提示,下面是代码 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>验证单选按钮组是否选择 IT知道网itwis.com</title> <script language="javascript"> function yanzheng(){ var theCheckboxInputs=document.getElementsByName('gender'); for(var i=0;i<theCheckboxInputs.length;i++) { if(theCheckboxInputs[i].checked) return true; } alert('您还没有选中其中的一项'); return false; } </script> </head>
<body> <form id="form1" name="form1" method="post" action=""> <label> <input type="radio" name="gender" id="radio" value="radio" />男 </label> <label> <input type="radio" name="gender" id="radio2" value="radio2" />女 </label> <label> <input type="submit" name="Submit" value="提交" onclick="return yanzheng();"/> </label> </form> </body> </html>
|