function test(){
  if(document.getElementById("check1").checked 
    && document.getElementById("text1").value=="")
    return false;
  if(document.getElementById("check2").checked 
    && document.getElementById("text2").value=="")
    return false;
  return true;
}

解决方案 »

  1.   

    <html>
    <body>
    <input type=checkbox id=check1> <input id=text1><br>
    <input type=checkbox id=check2> <input id=text2><br>
    <input type=button onclick="alert(test())">
    </body>
    <script language="javascript">
    function test(){
      if(document.getElementById("check1").checked 
        && document.getElementById("text1").value=="")
        return false;
      if(document.getElementById("check2").checked 
        && document.getElementById("text2").value=="")
        return false;
      return true;
    }
    </script>
    </html>
    可能和楼主期望的效果有差距^_^
      

  2.   

    我的代码是这样的:<html>
    <body>
    <div align="center">
    <table border="0" width="780">
    <tr>
    <td align="right">新闻类型:</td>
    <td width="655">
    <input type=checkbox id=check1 name="C1" value="ON">头条新闻</td>
    </tr>
    <tr>
    <td valign="top" align="right">简介:</td>
    <td width="655">
    <form method="POST" action="#">
    <p><textarea rows="5" name="S1" cols="20"></textarea></p>
    <p><input type="submit" value="提交" name="B1">
    <input type="reset" value="重置" name="B2"></p>
    </form>
    </td>
    </tr>
    </table>
    </div>
    </body>
    </html>就是当勾选复选框C1后,文本框S1必须填写,否则提示"文本框内容须填写".我要的就是这个验证代码.
      

  3.   

    <form ... onsubmit="return check()">
    <script>
    function check(){
      if(document.getElementById("check1").checked && document.all.S1.value=="")
        return false;
      return true;
    }
    </script>
    建议早该把代码贴出来了,这样也不会绕弯
      

  4.   

    function check(form)
    {
    if(form.S1.value==""&& document.getElementById("check1").checked==true)
    {
    alert("不能为空");
    return false;
    }
    }<form onsubmit="return check(this)"
      

  5.   

    按照楼上的,好像不行啊:<html>
    <body>
    <script language="javascript">
    function check(form1)
    {
    if(form.S1.value==""&& document.getElementById("c1").checked==true)
    {
    alert("不能为空");
    return false;
    }
    }<form onsubmit="return check(this)"
    </script>
    <div align="center">
    <table border="0" width="780">
    <tr>
    <td align="right">新闻类型:</td>
    <td width="655">
    <input type=checkbox id=check1 name="C1" value="ON">头条新闻</td>
    </tr>
    <tr>
    <td valign="top" align="right">简介:</td>
    <td width="655">
    <form method="POST" action="#" name="form1">
    <p><textarea rows="5" name="S1" cols="20"></textarea></p>
    <p><input type="submit" value="提交" name="B1">
    <input type="reset" value="重置" name="B2"></p>
    </form>
    </td>
    </tr>
    </table>
    </div>
    </body>
    </html>
      

  6.   

    <html>
    <body>
    <script language="javascript">
    function check(){
      if(document.getElementById("check1").checked && document.all.S1.value=="")
        return false;
      return true;
    }
    </script>
    <div align="center">
    <table border="0" width="780">
    <tr>
    <td align="right">新闻类型:</td>
    <td width="655">
    <input type=checkbox id=check1 name="C1" value="ON">头条新闻</td>
    </tr>
    <tr>
    <td valign="top" align="right">简介:</td>
    <td width="655">
    <form method="POST" action="#" name="form1" onsubmit="return check()">
    <p><textarea rows="5" name="S1" cols="20"></textarea></p>
    <p><input type="submit" value="提交" name="B1">
    <input type="reset" value="重置" name="B2"></p>
    </form>
    </td>
    </tr>
    </table>
    </div>
    </body>
    </html>两个都不行,帮我看看哪里错
      

  7.   

    结贴,搞定了<html>
    <body>
    <script language="javascript">
    function checkForm(form)
    {
      if(form.C1.checked && form.S1.value == "")
      {
        alert('文本框内容须填写');
        return false;
      }
    }
    </script>
    <div align="center">
    <form method="POST" action="#" onsubmit="return checkForm(this);">
    <table border="0" width="780">
    <tr>
    <td align="right">新闻类型:</td>
    <td width="655">
    <input type=checkbox id=check1 name="C1" value="ON">头条新闻</td>
    </tr>
    <tr>
    <td valign="top" align="right">简介:</td>
    <td width="655">

    <p><textarea rows="5" name="S1" cols="20"></textarea></p>
    <p><input type="submit" value="提交" name="B1">
    <input type="reset" value="重置" name="B2"></p>

    </td>
    </tr>
    </table>
    </div></form>
    </body>
    </html>