您的位置:首页 > Web前端 > JQuery

js/jquery实现checkbox全选和全不选代码总结

2013-09-05 20:24 741 查看
javascript 实现checkbox全选和全不选代码总结,有需要的朋友可参考一下本文章。代码分析
html代码
<form name="myform" method="post" id="myform" action="" >  
<input name='id' type='checkbox' onclick='unselectall()' id='id' value='1'> 设计家园  
<input name='id' type='checkbox' onclick='unselectall()' id='id' value='2'> 网页教程  
<input name='id' type='checkbox' onclick='unselectall()' id='id' value='3'> 酷站欣赏  
<input name='id' type='checkbox' onclick='unselectall()' id='id' value='4'> 网页素材  
<input name='chkAll' type='checkbox' id='chkAll' onclick='CheckAll(this.form)' value='checkbox'>  
全选  
</form>
js代码
<script language=javascript> 
function unselectall(){ 
if(document.myform.chkAll.checked){ 
document.myform.chkAll.checked = document.myform.chkAll.checked&0; 
} 
} 
function CheckAll(form){ 
for (var i=0;i<form.elements.length;i++){ 
var e = form.elements[i]; 
if (e.Name != 'chkAll'&&e.disabled==false) 
e.checked = form.chkAll.checked; 
} 
} 
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>js实现checkbox全选,反选,全不选</title>
    <script type="text/javascript">
        //复选框全选
        function checkAll(formvalue) {
            var roomids = document.getElementsByName(formvalue);
            for (var j = 0; j < roomids.length; j++) {
                if (roomids.item(j).checked == false) {
                    roomids.item(j).checked = true;
                }
            }
        }
        //复选框全不选
        function uncheckAll(formvalue) {
            var roomids = document.getElementsByName(formvalue);
            for (var j = 0; j < roomids.length; j++) {
                if (roomids.item(j).checked == true) {
                    roomids.item(j).checked = false;
                }
            }
        }
        //复选框选择转换
        function switchAll(formvalue) {
            var roomids = document.getElementsByName(formvalue);
            for (var j = 0; j < roomids.length; j++) {
                roomids.item(j).checked = !roomids.item(j).checked;
            }
        }
    </script>
</head>
<body>
  <input type="radio" name="all" id="all" onclick="checkAll('test')" />
    全选
    <input type="radio" name="all" id="Checkbox1" onclick="uncheckAll('test')" />
    全不选
    <input type="radio" name="all" id="Checkbox2" onclick="switchAll('test')" />
    反选<br />
    <sel
    <input name="test" value="复选框1" type="checkbox" />复选框1<br />
    <input name="test" value="复选框2" type="checkbox" />复选框2<br />
    <input name="test" value="复选框3" type="checkbox" />复选框3<br />
    <input name="test" value="复选框4" type="checkbox" />复选框4<br />
    <input name="test" value="复选框5" type="checkbox" />复选框5<br />
    <input name="test" value="复选框6" type="checkbox" />复选框6<br />
</body>
</html>
jquery代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>jQuery实现CheckBox全选、全不选</title>
<script src="http://code.jquery.com/jquery-1.4.4.min.js" type="text/javascript"></script>    <script type="text/javascript">
        $(function() {
           $("#checkAll").click(function() {
                $('input[name="subBox"]').attr("checked",this.checked); 
            });
            var $subBox = $("input[name='subBox']");
            $subBox.click(function(){
    $("#checkAll").attr("checked",$subBox.length == $("input[name='subBox']:checked").length ? true : false);
            });
        });
    </script>
</head>
<body>
    <div>
        <input id="checkAll" type="checkbox" />全选
        <input name="subBox" type="checkbox" />项1
        <input name="subBox" type="checkbox" />项2
        <input name="subBox" type="checkbox" />项3
        <input name="subBox" type="checkbox" />项4
    </div>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: