全选复选框 [英] Select All Checkbox
本文介绍了全选复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个网页以表格/形式返回搜索结果。我想有一个全选复选框,将选择所有的搜索结果复选框。显示结果的代码如下:
I have a webpage that returns search results in a table/form. I would like to have a select all checkbox, that would select all the checkboxes for the search results. My code for the display results is below:
<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>
我已尝试使用以下函数:
I have tried using the following function:
<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">;
但我不能让它工作。
推荐答案
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;
}
这篇关于全选复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文