簡單的js反選,全選,全不選

木子小僧發表於2015-12-15
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'check.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <input type="checkbox" id="boxid" onclick="selAllNo()">全選/全不選
    <hr>
    <input type="checkbox" name="love"/>籃球
    <input type="checkbox" name="love"/>足球
    <input type="checkbox" name="love"/>乒乓球
    <input type="checkbox" name="love"/>排球
    <input type="checkbox" name="love"/>保齡球
    <hr>
    <input type="button" name="checkedAll" onclick="selAll()" value="全選">
    <input type="button" name="checkedNo" onclick="selNo()" value="全不選">
    <input type="button" name="checkedOther" onclick="selOther()" value="反選">
    <script type="text/javascript">
        function selAll(){
            var loves = document.getElementsByName("love");
            
            for(var i=0;i<loves.length;i++){
                var love1 = loves[i];
                love1.checked = true;
            }
        }
        function selNo(){
            var loves = document.getElementsByName("love");
            
            for(var i=0;i<loves.length;i++){
                var love1 = loves[i];
                love1.checked = false;
            }
        }
        function selOther(){
            var loves = document.getElementsByName("love");
            for(var i=0;i<loves.length;i++){
                var love1 = loves[i];
                if(love1.checked==true){
                    love1.checked = false;
                }else{
                    love1.checked = true;
                }
            }
            
        }
        function selAllNo(){
            var boxid = document.getElementById("boxid");
            if(boxid.checked==true){
                selAll();
            }else{
                selNo();
            }
        }
    </script>
  </body>    
</html>

 

相關文章