【发布时间】:2013-01-09 11:47:04
【问题描述】:
我在表格中使用复选框,并且我在表头中有一个复选框。
我想要完成的是,当我点击头部的复选框时,表格行下面的所有复选框都应该被选中..
这是我的 HTML:
<form action="<?php $_PHP_SELF ?>" method="post">
<div class="content top">
<table id="datatable_example" class="responsive table table-striped table-bordered" style="width:100%;margin-bottom:0; ">
<thead>
<tr>
<th style="width:0px; padding-right:0px;" class="no_sort"> <label class="checkbox ">
<input type="checkbox" id="checkAll" name="checkAll" class="select_all">
</label>
</th>
<th style="width:200px;" class="no_sort"> Institue </th>
<th style="width:150px;" class="no_sort"> Education </th>
<th style="width:300px;" class="no_sort"> Description </th>
<th style="width:150px;" class="ue no_sort"> Started </th>
<th style="width:150px;" class="ue no_sort"> Completion </th>
<th class="ms no_sort "> Actions </th>
</tr>
</thead>
<tbody>
<?php echo $educations ?>
</tbody>
</table>
<div class="row-fluid control-group">
<div class="pull-left span6 " action="#">
<div>
<div class="controls inline input-large pull-left">
<select name="bulkaction" data-placeholder="Bulk actions: " class="chzn-select " id="default-select">
<option value=""></option>
<option value="delete">Delete</option>
</select>
</div>
<button type="submit" name="submitbulkaction" class="btn btn-inverse inline">Apply</button></form>
这是表格中使用的 php 变量 $education..
$educations .= "<tr>
<td><label class=\"checkbox\">
<input type=\"checkbox\" class=\"chkboxes\" name=\"ids[]\" value=\"$education_id\">
</label></td>
<td class=\"to_hide_phone\"> $institue_name</td>
<td class=\"to_hide_phone\"> $education_name</td>
<td>$education_desc</td>
<td>$education_started</td>
<td>$education_ended</td>
<td class=\"ms\">
<div class=\"btn-group1\">
<a href=\"education-edit.php?id=$education_id\" class=\"btn btn-small\" rel=\"tooltip\" data-placement=\"left\" data-original-title=\" edit \">
<i class=\"gicon-edit\"></i></a>
<a class=\"btn btn-small\" rel=\"tooltip\" data-placement=\"top\" data-original-title=\"View\">
<i class=\"gicon-eye-open\"></i>
</a>
<a class=\"btn btn-small\" rel=\"tooltip\" data-placement=\"bottom\" data-original-title=\"Remove\"><i class=\"gicon-remove \"></i></a>
</div>
</td>
</tr>";
我正在尝试设置后面的 Jquery..
<script type="text/javascript">
$(document).ready(function(){
$('#checkAll').click(function () {
$('.chkboxes').attr('checked','checked');
});
});
</script>
请告诉我我在这里做错了什么,以及如何解决问题?
我也查看了关于相同问题复选框的其他问题,其中大多数问题都得到了解决,但是当我应用相同的代码时仍然没有得到我想要的结果。
更新:正如你们所说,我修复了类名错误,谢谢, 但现在出现了一些奇怪的问题。 我的意思是现在它可以工作,但它不像我点击复选框并且所有复选框都在旅途中被选中。我必须刷新页面然后所有复选框都会被选中? 那个问题是怎么回事? 为什么我需要刷新页面才能发生该事件?
【问题讨论】:
-
StackOverflow 不适合回答这个问题。我们不进行代码调试。您需要进行自己的调试,如果您不确定为什么某些事情没有按预期工作,请发布代码并说明您期望它做什么,以及实际在做什么,包括所有错误消息。见ask advice。
-
据我所见,您正在尝试使用
.checkboxes,而在您的PHP代码中您正在使用.chkboxes -
@MarcusRecck 谢谢,我更新了我的错误,但现在出现了新问题。当我单击复选框以 selectAll 时,它当时没有被选中,但是当我单击重新加载按钮时,所有复选框都被选中,因为我在刷新页面之前单击了复选框。
-
尝试实现答案部分中的示例代码之一。
标签: php jquery checkbox html-table