HTML 行表选择
HTML Row table selection
我有一个表格,其中第一列是一个复选框,用于选择当前行,标题有一个 SelectAll 复选框。当用户单击该行时,该表也会被选中(复选框变为选中状态)。
我的问题是,当我全选时,所有复选框都被选中,但行也没有被选中。
我的复选框具有更改功能,因此我假设当我点击全选时,更改方法也会执行,但这不会发生。
$(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>
JS小提琴在这里
您可以通过在点击事件中触发更改事件来解决此问题:
$('#selectAll').click(function(event) {
if (this.checked) {
// Iterate each checkbox
$(':checkbox').each(function() {
this.checked = true;
}).change();
} else {
$(':checkbox').each(function() {
this.checked = false;
}).change();
}
});
$(document).ready(function() {
$('#selectAll').click(function(event) {
if (this.checked) {
// Iterate each checkbox
$(':checkbox').each(function() {
this.checked = true;
}).change();
} else {
$(':checkbox').each(function() {
this.checked = false;
}).change();
}
});
$('.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>
工作小提琴
您可以添加类highlight_row
并在单击事件中删除selectAll
:
$('#selectAll').click(function(event) {
if(this.checked) {
// Iterate each checkbox
$(':checkbox').each(function() {
this.checked = true;
$(this).closest('tr').addClass('highlight_row');
});
}
else {
$(':checkbox').each(function() {
this.checked = false;
$(this).closest('tr').removeClass('highlight_row');
});
}
});
希望这有帮助。
$(document).ready(function () {
$('#selectAll').click(function(event) {
if(this.checked) {
// Iterate each checkbox
$(':checkbox').each(function() {
this.checked = true;
$(this).closest('tr').addClass('highlight_row');
});
}
else {
$(':checkbox').each(function() {
this.checked = false;
$(this).closest('tr').removeClass('highlight_row');
});
}
});
$('.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="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.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>
我只会触发更改,而不是在多个地方有逻辑
$('#selectAll').click(function(event) {
$(':checkbox').prop("checked",this.checked).change();
});
$("input[type='checkbox']").change(function (e) {
$(this).closest('tr').toggleClass("highlight_row", this.checked);
});
现在我要为 html 做的是你应该使用 thead 和 tbody,而不是使用一个类来表示你有一个 head 行。
JsFiddle
对工作示例进行了小调整,将click
事件调度到需要更改的复选框。这应该会正确触发这些框的 onchange
事件
新部分如下所示:
if(this.checked) {
// Iterate each checkbox
$(':checkbox').not(":checked").click()
}
else {
$(':checkbox:checked').click()
}
相关文章:
- jQuery在两个字符串标识符之间选择HTML
- D3选择html——传递函数时,索引从1开始,而不是从0开始
- 如何减去选项两个相关的选择 HTML
- 选择 html 表中的行
- 使用jquery聚焦选择html元素
- jQuery从选项的输出字符串中选择HTML选项
- 使用 jQuery 通过 ID 属性选择 HTML 元素
- 尝试开机自检“选择”HTML表单输入时出现PHP错误
- 从单选按钮中获取值以选择 html 格式的图像
- 如何使用javascript/jquery中的某个类选择HTML页面上的所有角度字段
- 仅选择 html 节点的根项
- 有没有办法通过javascript注入自动选择HTML<选项>
- 如何选择 html <选项>其中 text=Washington
- 如何选择 HTML 表中一列的所有复选框
- 通过jquery动态创建和选择html元素
- 有没有其他方法可以选择HTML元素
- 如何调用一个jQuery Colorbox弹出在选择HTML控件的onchange事件
- 如何使用 jsoup 选择 html 文档的叶标签
- 使用 HTML 选择 (HTML) 的选择顺序出现问题
- 在选择 HTML 表单的文本输入字段后无法使日期选取器工作