checkbox的全选、全不选

发表于:2007-05-25来源:作者:点击数: 标签:
META http-equiv=Content-Type content="text/html; charset=gb2312" SCRIPT language= java script !-- 全选 -- function selectall(v){ var f = document.forms["resultlist"]; for (i=0;if.elements.length;i++) if (f.elements[i].name=="userid") f.ele

<META http-equiv=Content-Type content="text/html; charset=gb2312">
<SCRIPT language=javascript>
<!-- 全选 -->
function selectall(v){
  var f = document.forms["resultlist"];
  for (i=0;i<f.elements.length;i++)
    if (f.elements[i].name=="userid") f.elements[i].checked = v;
  document.forms["resultlist"].elements["clickall"].checked = v;
  document.forms["resultlist"].elements["clickall2"].checked = v;
}


<!-- 改变选择列的颜色 -->
function changechecked(checkbut){
 var v =checkbut.checked;
 
 if (v) {
     checkbut.checked = true;
     if(isallselected()){
      document.forms["resultlist"].elements["clickall"].checked = true;
      document.forms["resultlist"].elements["clickall2"].checked = true;
     }
    }else{
     document.forms["resultlist"].elements["clickall"].checked = false;
     document.forms["resultlist"].elements["clickall2"].checked = false;
     checkbut.checked = false;
    }
}


<!-- 是否有选中? -->
function isselected(){
  var f = document.forms["resultlist"];
  for (i=0;i<f.elements.length;i++)
    if ((f.elements[i].name=="userid") && (f.elements[i].checked)) return true;
  return false;
}


<!--是否全部选中? -->
function isallselected(){
 var selectcount = 0;
 var allcount=0;
    var f = document.forms["resultlist"];
 for(i=0;i<f.elements.length;i++){
  if (f.elements[i].checked){
   selectcount = selectcount + 1;
  }
 }
 for(i=0;i<f.elements.length;i++){
  if ((f.elements[i].name.indexOf("select")>-1)){
   allcount = allcount+1;
  }
 }
 if(selectcount==allcount)
  return true;
 else
  return false;
}
</SCRIPT>


<FORM name=resultlist action=list method=post>
<INPUT onclick=selectall(this.checked) type=checkbox name=clickall>全选
<INPUT onclick=changechecked(this) type=checkbox value=on name=userid>
<INPUT onclick=changechecked(this) type=checkbox value=on name=userid>
<INPUT onclick=changechecked(this) type=checkbox value=on name=userid>
<INPUT onclick=changechecked(this) type=checkbox value=on name=userid></TD>
<INPUT onclick=selectall(this.checked) type=checkbox  name=clickall2>全选
</FORM>

原文转自:http://www.ltesting.net