为什么没有选中复选框?

时间:2014-01-18 14:24:20

标签: javascript php

这是我的代码:

<html>
<head>
<script type="text/javascript" language="javascript">
function checkAll(formname, checktoggle)
{
    var checkboxes = new Array();
    checkboxes = document.forms[formname].getElementsByTagName("input");
    for (var i=0; i<checkboxes.length; i++)  {
        if (checkboxes[i].type == "checkbox")   {
            checkboxes[i].checked = checktoggle;
        }
    }
}
</script>
</head>

<body bgcolor='lightgray'>
    <a onclick="javascript:checkAll('myform',true);" href="#">check all</a>
<?php
    while($rows=mysql_fetch_assoc($sql)) {
        echo "<form name='myform'>";
        echo "<input type='checkbox' id='chk' name='chk' /> $rows[subject]";
        echo "</form>";
    }
?>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

试试这个:

<?php

    echo "<form name='myform'>";
    $n = 1;
    while($rows=mysql_fetch_assoc($sql)) {

        echo "<input type='checkbox' id='chk" . $n++  . "' name='chk' /> $rows[subject]";

    }
    echo "</form>";

?>

<form>....</form>置于循环之外,否则您将获得多个名称为“myform”的表单,然后在您的javascript中无法正确处理,请参阅DarkBee的评论