javascript全选反选功能

<script type="text/javascript" language="javascript">
        function selectBox(selectType){
        var checkboxis = document.getElementsByName("Checkbox");
        if(selectType == "reverse"){
            for (var i=0; i<checkboxis.length; i++){
                //alert(checkboxis[i].checked);               
                //checkboxis[i].checked = !checkboxis[i].checked;
    checkboxis[i].checked = false;
            }
        }
        else if(selectType == "all")
        {
  //alert(document.getElementsByName("Checkbox").length);
            for (var i=0; i<checkboxis.length; i++){
                //alert(checkboxis[i].checked);               
                checkboxis[i].checked = true;
            }
        }
     }
    
     function deleteSelect(){       
        var aryId = [];
        var checkboxis = document.getElementsByName("Checkbox");
        for (var i=0; i<checkboxis.length; i++){
            //alert(checkboxis[i].checked);
            if(checkboxis[i].checked)
                aryId.push(checkboxis[i].value);
        }
        if(aryId.length > 0)
        {
            if(confirm("该操作比较危险请慎重

确实要删除吗"))
            location.href = "deleteit.php?action=deleteQuestion&aryId=" + aryId.join(",");
   else
   //alert("您取消了");
   for (var i=0; i<checkboxis.length; i++){
            //alert(checkboxis[i].checked);               
            //checkboxis[i].checked = !checkboxis[i].checked;
   checkboxis[i].checked = false;
            }
        }
  else
        {
            alert("请选择要删除的记录!");
        }   
     }
     </script>

<form name="form1" action="" method="post">

<input type=checkbox name=checkbox id=checkbox value=1>

<input type=checkbox name=checkbox id=checkbox value=2>

<input type=checkbox name=checkbox id=checkbox value=3>

<input type=checkbox name=checkbox id=checkbox value=4>

<input type=checkbox name=checkbox id=checkbox value=5>

<input type=checkbox name=checkbox id=checkbox value=6>

<input id="Button3" type="button" value="全选" onClick="selectBox('all')" />
  <input id="Button1" type="button" value="反选" onClick="selectBox('reverse')" />
  <input id="Button2" type="button" value="删除所选" onClick="deleteSelect()" />

</form>

    A+
发布日期:2009年08月19日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: