当前位置: 代码迷 >> JavaScript >> js兑现checkbox全选,反选,全不选
  详细解决方案

js兑现checkbox全选,反选,全不选

热度:131   发布时间:2012-09-22 21:54:54.0
js实现checkbox全选,反选,全不选
<!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>
?
  相关解决方案