js 全选/反选代码

发布时间:2008年07月25日      浏览次数:1157 次
==============================================
方法一:
==============================================
<script>
function checkAll(str)
{
      if(str != undefined)
      {
            if(str.length == undefined)
            {
                  if(str.checked == true)
                  str.checked = false;
                  else
                  str.checked = true;
            }else{
                   for(var i=0;i<str.length;i++)
                   {
                         if(str[i].checked == true)
                               str[i].checked = false;
                         else
                               str[i].checked = true;
                   }
            }
      }
}
</script>
调用示例:
<input type="checkbox" name="chk_all" value="checkbox" onclick="checkAll(document.all.k)" /> 全选/反选
<input type="checkbox" name="k" value="" />
<input type="checkbox" name="k" value="" />
<input type="checkbox" name="k" value="" />
<input type="checkbox" name="k" value="" />
==============================================
兼容【垃圾火狐浏览器】的写法:
==============================================
<script>
//全选
function checkAll(this_str,str)
{
      obj_this_str=document.getElementById(this_str);
      obj_str_name=document.getElementsByName(str);
      if( obj_str_name.length > 0){
            for(var i=0;i<obj_str_name.length;i++){
                   if(obj_this_str.checked == true)
                         obj_str_name[i].checked = true;
                   else
                         obj_str_name[i].checked = false;
            }
      }
}
//反选
function check_reverse(str)
{
      obj_str_name=document.getElementsByName(str);
      if( obj_str_name.length > 0){
             for(var i=0;i<obj_str_name.length;i++){
                   if(obj_str_name[i].checked == true)
                         obj_str_name[i].checked = false;
                   else
                         obj_str_name[i].checked = true;
             }
      }
}
</script>
<input type="checkbox" name="chk_all" id="chk_all" value="checkbox" onclick="checkAll('chk_all','k')" />
全选/
<input type="checkbox" name="chk_reverse" id="chk_reverse" value="checkbox" onclick="check_reverse('k')" />
反选
<input type="checkbox" name="k" id="k" value="" />
<input type="checkbox" name="k" id="k" value="" />
<input type="checkbox" name="k" id="k" value="" />
<input type="checkbox" name="k" id="k" value="" />
==============================================
方法二:
==============================================
script language="javascript">
function SelectIterm(form,sign){
for (var i=0; i<form.elements.length;i++ ){
if (form.elements[i].type == "checkbox"){
var e=form.elements[i];
if (sign==0) e.checked= true;
if (sign==1) e.checked= !e.checked;
if (sign==2) e.checked= false;
}
}
}
</script>
调用代码如下(FormName为表单的名称):
<a href="javascript:SelectIterm(FormName,0);">全选</a>
<a href="javascript:SelectIterm(FormName,1);">反选</a>
<a href="javascript:SelectIterm(FormName,2);">取消</a>
文章来源:桂林唯创网络
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!