首页 > 代码库 > Javascript中的checked

Javascript中的checked


示例代码:全选、全不选、反选

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>Javascript测试</title>
    <script type="text/javascript">
        onload=function(){
            document.getElementById(‘btnSelectAll‘).onclick=function(){
                var inputObjs = document.getElementsByTagName("input");
                for(var i=0;i<inputObjs.length;i++)
                {
                    if(inputObjs[i].type=="checkbox")
                    {
                        inputObjs[i].checked = true;
                    }
                }
            };
            document.getElementById(‘btnSelectNone‘).onclick=function(){
                var inputObjs = document.getElementsByTagName("input");
                for(var i=0;i<inputObjs.length;i++)
                {
                    if(inputObjs[i].type=="checkbox")
                    {
                        inputObjs[i].checked = false;
                    }
                }
            };
            document.getElementById(‘btnSelectReverse‘).onclick=function(){
                var inputObjs = document.getElementsByTagName("input");
                for(var i=0;i<inputObjs.length;i++)
                {
                    if(inputObjs[i].type=="checkbox")
                    {
                        inputObjs[i].checked = !inputObjs[i].checked;
                    }
                }
            };
        };
    </script>
</head>
<body>
    <input type="button" id="btnSelectAll" value="http://www.mamicode.com/全选"/>
    <input type="button" id="btnSelectNone" value="http://www.mamicode.com/全不选"/>
    <input type="button" id="btnSelectReverse" value="http://www.mamicode.com/反选"/>
    <input type="checkbox" value="http://www.mamicode.com/1"/>吃饭
    <input type="checkbox" value="http://www.mamicode.com/2"/>睡觉
    <input type="checkbox" value="http://www.mamicode.com/3"/>跑步
    <input type="checkbox" value="http://www.mamicode.com/4"/>读书
    <input type="checkbox" value="http://www.mamicode.com/5"/>看电影
</body>
</html>

效果图

技术分享






Javascript中的checked