选择“全部复选框”

Select All Checkbox

本文关键字:全部复选框 复选框 全部 选择      更新时间:2023-09-26

我有一个网页,返回一个表/表单的搜索结果。我想有一个选择所有复选框,这将选择所有复选框的搜索结果。我用于显示结果的代码如下:

<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!

您可以使用jquery以更简单的方式完成此操作

$("form input:checkbox").attr('checked','true');
var form = document.getElementsById('form_id');
var checkBoxes = form.getElementsByTagName('input');
for (var i in checkBoxes)
{
   if (checkBoxes[i].type=="checkbox")
      checkBoxes[i].checked = true; 
}

最好的解决方案是使用Jquery脚本

/*Unik Checkbox to mark/desmark other checkboxes*/
<input type="checkbox" onclick="selectAllCheckBoxes(this)" id="checkBoxUnik"/>
/*Several other Checkboxes inside loop*/
<input type="checkbox" onclick="unselectCheckBoxUnik()" class="checkBoxInLoop" />
<script>
    function selectAllCheckBoxes(obj){
        $('input:checkbox:not(:disabled).checkBoxInLoop').prop('checked', jQuery(obj).prop('checked'));
    }
    function unselectCheckBoxUnik(){
        /*first you verify if the quantity of checkboxes equals number of checkboxes*/
        a = $('input:checkbox:not(:disabled).checkBoxInLoop:checked').size();
        b = $('input:checkbox:not(:disabled).checkBoxInLoop').size();
        /*if equals, mark a checkBoxUnik*/
        ((a == b)? $('#checkBoxUnik').prop("checked", true) : $('#checkBoxUnik').prop("checked", false));
    }
</script>