checkbox的全选、全不选

类别:网站制作 点击:0 评论:0 推荐:

<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://com.8s8s.com/it/it30910.htm