<html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
 <title>聊天室登陆</title>
 </head>
 <script language="JavaScript">
 function check(){
  if(chatform.username.value==''){
  alert("请输入昵称!");
  return false;
  }
  if(chatform.room.value==''){
  alert("请选择房间!");
  return false;
  }
  return true;
 }
 </script> <body>
 <form action="chat.php" name="chatform" method="post" onSubmit="return check()">
 昵称:
 <input type="text" name="username" value="" size=15>
 <input type="radio" name="room" value=1>一号房间
 <input type="radio" name="room" value=2>二号房间
 <br><br>
 <input type="submit" name="submit" value="登陆">
 </form>
 </body>
 </html>请问 当 昵称不为空时,而房间为空,为什么程序还能执行到CHAT.PHP呀?

解决方案 »

  1.   

    你自己最后都是返回的True啊  当然会提交啦! 加个else 就可以了
      

  2.   

    <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
    <title>聊天室登陆 </title> 
    </head> 
    <script language="JavaScript"> 
    function check(){ 
    if(chatform.username.value==''){ 
    alert("请输入昵称!"); 
    return false; 

     if(!document.chatform.room1.checked&&!document.chatform.room2.checked)   
      {   
      alert("请选择房间");   
      return   false;   
      }   
    return true; 

    </script> <body> 
    <form action="chat.php" name="chatform" method="post" onSubmit="return check()"> 
    昵称: 
    <input type="text" name="username" value="" size=15> 
    <input type="radio" name="room1" value=1>一号房间 
    <input type="radio" name="room2" value=2>二号房间 
    <br> <br> 
    <input type="submit" name="submit" value="登陆"> 
    </form> 
    </body> 
    </html> 
      

  3.   

    调试时先看看room.value是什么,然后再判断
    room不是text,不能简单的用value==''判断
      

  4.   

    呵呵,如果把人家的名字改了,那么这两个radio就不是一个组了,难道你让人家都可以选?
    呵呵。
    一般的做法都是:
    var radio=document.getElementsByName("room");
    然后遍历
    设置个标示位,
    判断radio[i].checked,如果都没有选中,则返回false。
      

  5.   

    <script language="JavaScript"> 
    function check(){ 
    if(chatform.username.value==''){ 
    alert("请输入昵称!"); 
    return false; 

    var rooms = document.getElementsByName("room");
    var r = false;
    for(var i=0;i<rooms.length;i++){
    if(rooms[i].checked){ 
    r=true;
     }
    }
    if(!r) {alert("选择房间"); return false;}return true; 

    </script>