把下面的javascript代码房子html的<head></head>里面

        <script type="text/javascript">
        function checkAll() {      
            var inputs = document.getElementsByName('hosts[]');
            var inputsLen = inputs.length;
            for (var i = 0; i < inputsLen ; i++ )
            {
                if (inputs[i].type.toLowerCase() == 'checkbox')
                {
                    inputs[i].checked == true 
                        ? inputs[i].checked = true
                        : inputs[i].checked = true;   
                }
            }
     
        }
        function clearAll() {        
            var inputs = document.getElementsByName('hosts[]');
            var inputsLen = inputs.length;
            for (var i = 0; i < inputsLen ; i++ )
            {
                if (inputs[i].type.toLowerCase() == 'checkbox')
                {
                    inputs[i].checked == false
                        ? inputs[i].checked = false
                        : inputs[i].checked = false;
                }
            }
     
        }
        </script>

以下是html代码,放在<body></body>里面

  1. <form action="echo.php" method=POST>
  2.     <input type="checkbox" name="hosts[]" value=1.1.1.1>
            1.1.1.1<br>
        <input type="checkbox" name="hosts[]" value=1.1.1.2>
            1.1.1.2<br>
        <input type="checkbox" name="hosts[]" value=1.1.1.3>
            1.1.1.3<br>
        <input type=button name=checkall value=全选 type=button name=clearall value=取消 onclick=clearAll()><b
    r>
        <input type=submit value=提交>
  3. </form>

 

提交后交给echo.php程序处理.

 

  1. <?php  
  2.  
  3. $hosts = $_POST['hosts'];  
  4.  
  5. foreach ($hosts as $i) {  
  6.    echo $i."<br>";    
  7. }  
  8.  
  9. ?>