把下面的javascript代碼房子html的<head></head>裏面javascript
<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>裏面php
- <form action="echo.php" method=POST>
- <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=提交>- </form>
提交後交給echo.php程序處理.html
- <?php
- $hosts = $_POST['hosts'];
- foreach ($hosts as $i) {
- echo $i."<br>";
- }
- ?>