问题说明:请照我下面的JSP网页代码,详细写一个能够判断单选按钮有没有选中任何一个的javascript,可以插在我的网页代码的头部。我要一开始所有单选按钮都没有选中,要求录入者来从四个中任选其中一个,如果录入者没有选中任何一个,在提交表单的时候会弹出对话框提示。各位大哥大姐,懂这个的,有劳了,不会少给分你的!谢谢!下面是我的网页代码:<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>成绩录入</title>
</head><body>
<form id="form1" name="form1" method="post" action="add_cj.jsp">
  <table width="416" height="162" border="0" cellpadding="1" cellspacing="1">
    <tr>
      <td width="115" align="center">音乐</td>
      <td width="294"><input type="radio" name="yingye" value="radiobutton" />
        优
        <input type="radio" name="yingye" value="radiobutton" />
        良
        <input type="radio" name="yingye" value="radiobutton" />
        及格
        <input type="radio" name="yingye" value="radiobutton" />
        不及格</td>
    </tr>
    <tr>
      <td align="center">美术</td>
      <td><input type="radio" name="meisu" value="radiobutton" />
        优
        <input type="radio" name="meisu" value="radiobutton" />
        良
        <input type="radio" name="meisu" value="radiobutton" />
        及格
        <input type="radio" name="meisu" value="radiobutton" />
        不及格</td>
    </tr>
    <tr>
      <td align="center">自然科学</td>
      <td><input type="radio" name="zirankexue" value="radiobutton" />
        优
        <input type="radio" name="zirankexue" value="radiobutton" />
        良
        <input type="radio" name="zirankexue" value="radiobutton" />
        及格
        <input type="radio" name="zirankexue" value="radiobutton" />
        不及格</td>
    </tr>
    <tr>
      <td colspan="2" align="center"><input name="tijiao" type="submit" id="tijiao" value="提交" /> 
        <input name="chongxie" type="reset" id="chongxie" value="重写" /></td>
    </tr>
  </table>
</form>
</body>
</html>

解决方案 »

  1.   

    你的每个选项的value不应该设为一样的..  <script   language="javascript">  
      function   check()  
      {  
      var   frm   =   document.forms[0];  
      var   value   =   0;  
      for   (var   i   =   0;   i   <   frm.a.length;   i   ++)  
      {  
      if   (frm.a[i].checked)  
      value   =   frm.a[i].value;  
      }  
      if   (value   ==   0)  
      alert('没有被选中的');  
      else  
      alert(value   +   '被选中了');  
      }  
      </script>   
      

  2.   

    楼主注意红色的部分。<html>
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
    <title>成绩录入 </title> 
    <script>
    function checkInput(){
    var yingyue = document.getElementsByName("yingye");
    var value1 = 0;
    for(var i=0;i<yingyue.length;i++){
    if(yingyue[i].checked)
    {
    value1 = yingyue[i].value;
    break;
    }
    }
    if(value1 == 0){
    alert("please choose ur score of yingyue");
    return false;
    }
    }
    </script>

    </head> <body> 
    <form id="form1" name="form1" method="post" action="add_cj.jsp"> 
      <table width="416" height="162" border="0" cellpadding="1" cellspacing="1"> 
        <tr> 
          <td width="115" align="center">音乐 </td> 
          <td width="294"> <input type="radio" name="yingye" value="4" /> 
            优 
            <input type="radio" name="yingye" value="3" /> 
            良 
            <input type="radio" name="yingye" value="2" /> 
            及格 
            <input type="radio" name="yingye" value="1" /> 
            不及格 </td> 
        </tr> 
        <tr> 
          <td align="center">美术 </td> 
          <td> <input type="radio" name="meisu" value="4" /> 
            优 
            <input type="radio" name="meisu" value="3" /> 
            良 
            <input type="radio" name="meisu" value="2" /> 
            及格 
            <input type="radio" name="meisu" value="1" /> 
            不及格 </td> 
        </tr> 
        <tr> 
          <td align="center">自然科学 </td> 
          <td> <input type="radio" name="zirankexue" value="4" /> 
            优 
            <input type="radio" name="zirankexue" value="3" /> 
            良 
            <input type="radio" name="zirankexue" value="2" /> 
            及格 
            <input type="radio" name="zirankexue" value="1" /> 
            不及格 </td> 
        </tr> 
        <tr> 
          <td colspan="2" align="center"> <input name="tijiao" type="submit" id="tijiao" value="提交" onClick="return checkInput();"/>  
            <input name="chongxie" type="reset" id="chongxie" value="重写" /> </td> 
        </tr> 
      </table> 
    </form> 
    </body> 
    </html> 
      

  3.   

    如果只是检查了弹对话框,value的值倒不一定要修改。但是你肯定后台要处理的,所以每一组radio里面应该value不同。
    这样在后台就可以 String yingyue = request.getParameter("yingye");