我有一个表格,第一列是复选框,用于选择当前行,标题栏还有一个全选复选框。当用户点击行时,表格也会被选中(复选框变为选中状态)。
我的问题是,当我全选时,所有复选框都被选中了,但是行没有被选中。
我的复选框具有更改函数,因此我认为当我选中全部时,更改方法也将执行,但实际上并没有发生。
$(document).ready(function() {
$('#selectAll').click(function(event) {
if (this.checked) {
// Iterate each checkbox
$(':checkbox').each(function() {
this.checked = true;
});
} else {
$(':checkbox').each(function() {
this.checked = false;
});
}
});
$('.record_table tr').click(function(event) {
if (event.target.type !== 'checkbox') {
$(':checkbox', this).trigger('click');
}
});
$("input[type='checkbox']").change(function(e) {
if ($(this).is(":checked")) {
$(this).closest('tr').addClass("highlight_row");
} else {
$(this).closest('tr').removeClass("highlight_row");
}
});
});
.record_table {
width: 100%;
border-collapse: collapse;
}
.record_table tr:hover {
background: #eee;
}
.record_table td {
border: 1px solid #eee;
}
.highlight_row {
background: #eee;
}
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<table class="record_table" id="table-patient-time">
<tr class="header">
<th>
<input type="checkbox" id="selectAll" />
</th>
<th>Hello</th>
<th>Hello</th>
</tr>
<tr class="selectable">
<td>
<input type="checkbox" />
</td>
<td>Hello</td>
<td>Hello</td>
</tr>
<tr>
<td>
<input type="checkbox" />
</td>
<td>Hello</td>
<td>Hello</td>
</tr>
<tr>
<td>
<input type="checkbox" />
</td>
<td>Hello</td>
<td>Hello</td>
</tr>
<tr>
<td>
<input type="checkbox" />
</td>
<td>Hello</td>
<td>Hello</td>
</tr>
</table>