页面代码为:
<form id="form1" name="form1" method="post" action="">
  <p>
    <label>
      <input name="checkbox" type="checkbox" id="checkbox" onclick="getemlemtID(this);" value="1"/>
    </label>
1
<label>
  <input name="checkbox1" type="checkbox" id="checkbox1" value="1" />
</label>
1</p>
  <p>
    <label>
      <input name="checkbox" type="checkbox" id="checkbox" value="2" onclick="getemlemtID(this);"/>
    </label>
2
<label>
  <input name="checkbox1" type="checkbox" id="checkbox1" value="2" />
</label> 
2
</p>
  <p>
    <label>
      <input name="checkbox" type="checkbox" id="checkbox" value="3" onclick="getemlemtID(this);"/>
    </label>
3
<label>
  <input name="checkbox1" type="checkbox" id="checkbox1" value="3" />
</label> 
3
</p>
</form>
我想点击checkbox值为1的复选框,checkbox1值为1的复选框也选中?

解决方案 »

  1.   

    function getemlemtID(obj)
    {
    if obj.value==1
    if(document.getElementById("checkbox1").value==1)
    document.getElementById("checkbox1").checkd=true;
    }
    这是个伪代码,我写了个大概意思。本机太慢了
      

  2.   

    楼主首先要告诉你id在整个页面中是唯一的,不能设置两个标签一样的id<!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>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
    $(document).ready(function(){
    var length = $(".checkbox").length;
    //alert(length)
    $(".checkbox").change(function(){
     var $checkbox1 = $(this).parent().next().children(".checkbox1");
     if($(this).is(":checked")){
      $checkbox1.attr("checked",true);
     }else{
    $checkbox1.attr("checked",false);
     }
    });
    $(".checkbox1").change(function(){
     var $checkbox = $(this).parent().prev().children(".checkbox");
     if($(this).is(":checked")){
      $checkbox.attr("checked",true);
     }else{
      $checkbox.attr("checked",false);
     }
    }); });</script>
    </head>
    <body>
    <form id="form1" name="form1" method="post" action="">
      <p>
      <label>
      <input name="checkbox" type="checkbox" class="checkbox" onclick="getemlemtID(this);" value="1"/>
      </label>
    1
    <label>
      <input name="checkbox1" type="checkbox" class="checkbox1" value="1" />
    </label>
    1</p>
      <p>
      <label>
      <input name="checkbox" type="checkbox" class="checkbox" value="2" onclick="getemlemtID(this);"/>
      </label>
    2
    <label>
      <input name="checkbox1" type="checkbox" class="checkbox1" value="2" />
    </label>  
    2
    </p>
      <p>
      <label>
      <input name="checkbox" type="checkbox" class="checkbox" value="3" onclick="getemlemtID(this);"/>
      </label>
    3
    <label>
      <input name="checkbox1" type="checkbox" class="checkbox1" value="3" />
    </label>  
    3
    </p>
    </form>
    </body>
    </html>
      

  3.   

    谢谢,,但是能不能让点击右边checkbox1的不影响左边的checkbox?