我陷入了一个非常愚蠢的问题。我试图选择我使用表中的循环动态创建的所有复选框。在表格的头部我有一个复选框,我想在点击头部的复选框后选中表格中的所有复选框。 这是我的代码。使用javascript的onclick检查复选框?
<table>
<thead>
<tr>
<th class="spacer_100 align_ctr tooltip_trigger">
Change Status <input type="checkbox" name="sample" onclick="selectAll()">
</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user) { ?>
<tr>
<td class="align_ctr" style="text-align: center">
<input type="checkbox" name="current_status[]" value="1"/>
</td>
</tr>
<?php } ?>
</tbody>
</table>
下面是javascript代码:
function selectAll() {
var checkboxes = document.getElementsByTagName("input");
for(var i=1; i<checkboxes.length;i++) {
if (checkboxes[i].type == 'checkbox') {
checkboxes[i].checked = 'true';
}
}
}
我不知道什么是错的这个代码,我不能够选择复选框。
感谢
[我的作品。(http://jsfiddle.net/bG3mm/) – icktoofay
顺便说一下,这里是你的foreach结束标记'<?php endforeach; ?>'? – Raymond
我在我的实际代码中有foreach的括号,我只是把它放在上面的代码中,但我仍然不确定为什么所有的复选框都没有选择。 – user2651516