编程语言
首页 > 编程语言> > php – 选择所有复选框

php – 选择所有复选框

作者:互联网

我有一个网页,以表格/表格的形式返回搜索结果.我想有一个全选复选框,可以选择搜索结果的所有复选框.我的显示结果代码如下:

<form action="noJavaScript.php" name="theForm" method="post">
<table style="border: 1px solid black" RULES=ALL FRAME=VSIDES>
<th> </th><th>Order #</th><th>Inspector</th><th>Reference #</th><th>Client Name</th><th>Property Address</th><th>City</th><th>State</th><th>Zip</th><th>Inspection Date</th>
        <?php
            while ($row = mysql_fetch_assoc($result))
            {
                echo '<tr><td>';
                echo '<input type="checkbox" name="selected[]" value="'.$row['order_number'].'"/>';
                echo '</td>';
                foreach ($row as $key => $value)
                    echo '<td>'.htmlspecialchars($value).'</td>';
                echo '</tr>';
            }
        ?>

    </table>
<input type="submit" name="submit" value="Edit/Modify Order" onClick="document.theForm.action='modify.php'">
<input type="submit" name="submit" value="Clone Order" onClick="document.theForm.action='clone.php'">
<input type="submit" name="submit" value="Delete Order" onClick="document.theForm.action='deleteorder.php'">
<input type="submit" name="submit" value="Archive Order" onClick="document.theForm.action='archive.php'">

</form>

我试过使用以下功能:

<script type="text/javascript"
<!--
function SetAllCheckBoxes(FormName, FieldName, CheckValue)
{
    if(!document.forms[FormName])
        return;
    var objCheckBoxes = document.forms[FormName].elements[FieldName];
    if(!objCheckBoxes)
        return;
    var countCheckBoxes = objCheckBoxes.length;
    if(!countCheckBoxes)
        objCheckBoxes.checked = CheckValue;
    else
        // set the check value for all check boxes
        for(var i = 0; i < countCheckBoxes; i++)
            objCheckBoxes[i].checked = CheckValue;
}
// -->
</script>

这样的按钮:

        <input type="button" onclick="SetAllCheckBoxes('theForm', 'myCheckbox', true);" value="Check All">;

但我无法让它发挥作用.

解决方法:

为每个复选框输入添加一个类.

echo’< input type =“checkbox”name =“selected []”value =“'.$row ['order_number'].'”class =“yourclass”/>‘;

然后在javascript中迭代文档中的所有输入字段,并检查它们是否同时具有type =“checkbox”和class =“yourclass”.并将所有这些设置为CHECKED!

标签:php,javascript,checkbox,selectall
来源: https://codeday.me/bug/20190630/1337440.html