<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script>
<!--
function CheckAll(){ 
    var obj = event.srcElement;
    var arr = document.getElementsByTagName("input");
    for(var i = 0;i<arr.length;i++){
        if(arr[i].type=="checkbox" && arr[i]!=obj){
            arr[i].checked = obj.checked;
        }
    }

-->
</script>
</head>
<body>
    <input type=checkbox name=apple>苹果</input><p>
    <input type=checkbox name=orange>橘子</input><p>
    <input type=checkbox name=banana>香蕉</input><p>
    <input type=checkbox name=grape>葡萄</input><p>
    <input type=checkbox name=peach>桃子</input><p>
    <input type=checkbox name=selectAll onclick="CheckAll()">全选</input><p> 
</body>
</html>

解决方案 »

  1.   


    <SCRIPT>
     function checkAll(){  //全选或全不选
          if(document.all.idcheckbox == null)
              return;
          var numRow = document.all.idcheckbox.length;
          if(numRow == null) {
            document.all.idcheckbox.checked = document.all.myCheckAll.checked;    
          }
          if(document.all.myCheckAll.checked) {
            for (var i = 0; i < numRow; i++) {
                document.all.idcheckbox[i].checked = true;
            }
        } else {
            for (var i = 0; i < numRow; i++) {
                document.all.idcheckbox[i].checked = false;
            }
        }
      }</SCRIPT><input name="idcheckbox" id="idcheckbox" type="checkbox" value="苹果">苹果</br>
    <input name="idcheckbox" id="idcheckbox" type="checkbox" value="橘子">橘子</br>
    <input name="idcheckbox" id="idcheckbox" type="checkbox" value="香蕉">香蕉</br>
    <input name="idcheckbox" id="idcheckbox" type="checkbox" value="葡萄">葡萄</br>
    <input name="idcheckbox" id="idcheckbox" type="checkbox" value="桃子">桃子</br>
    <input name="myCheckAll" type="checkbox"  value="checkbox" onClick=" checkAll();">全选/全不选</br>
      

  2.   

    <SCRIPT>
     function checkAll(){  //全选或全不选
    var str="";
          if(document.all.idcheckbox == null)
              return;
          var numRow = document.all.idcheckbox.length;
          if(numRow == null) {
            document.all.idcheckbox.checked = document.all.myCheckAll.checked;    
          }
          if(document.all.myCheckAll.checked) {
            for (var i = 0; i < numRow; i++) {
                document.all.idcheckbox[i].checked = true;
                str+=document.all.idcheckbox[i].value;        }
    alert("\n全选:\n\n"+str);
        } else {
            for (var i = 0; i < numRow; i++) {
                document.all.idcheckbox[i].checked = false;
            }
        }
      }</SCRIPT><input name="idcheckbox" id="idcheckbox" type="checkbox" value="苹果">苹果</br>
    <input name="idcheckbox" id="idcheckbox" type="checkbox" value="橘子">橘子</br>
    <input name="idcheckbox" id="idcheckbox" type="checkbox" value="香蕉">香蕉</br>
    <input name="idcheckbox" id="idcheckbox" type="checkbox" value="葡萄">葡萄</br>
    <input name="idcheckbox" id="idcheckbox" type="checkbox" value="桃子">桃子</br>
    <input name="myCheckAll" type="checkbox"  value="checkbox" onClick=" checkAll();">全选/全不选</br>
      

  3.   

    兼容FF的话需改一下。
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <script>
    <!--
    function CheckAll(sender)

    var arr = document.getElementsByTagName("input");
    for (var i = 0; i < arr.length; i++)
    {
    if (arr[i].type == "checkbox" && arr[i] != sender)
    {
    arr[i].checked = sender.checked;
            }
        }

    -->
    </script>
    </head>
    <body>
        <input type=checkbox name=apple>苹果</input><p>
        <input type=checkbox name=orange>橘子</input><p>
        <input type=checkbox name=banana>香蕉</input><p>
        <input type=checkbox name=grape>葡萄</input><p>
        <input type=checkbox name=peach>桃子</input><p>
        <input type=checkbox name=selectAll onclick="CheckAll(this)">全选</input><p> 
    </body>
    </html>
      

  4.   

    1.请问这个这句话什么意思?
     if(arr[i].type=="checkbox" && arr[i]!=obj){
    2.我如果想获得所有的复选框数量,除了通过这个方式document.getElementsByTagName("input");
      还有别的方式吗?
      

  5.   

     if(arr[i].type=="checkbox" && arr[i]!=obj)
    input有许多类型,只取其中的checkbox, 并且排除掉全选的那个checkbox