表格反选jquery
时间: 2023-07-24 19:18:04 浏览: 50
您可以使用以下代码来实现表格的反选功能:
```javascript
// 给反选按钮添加点击事件
$("#inverse-btn").click(function() {
// 遍历所有复选框
$("table input[type='checkbox']").each(function() {
// 将选中状态取反
$(this).prop("checked", !$(this).prop("checked"));
});
});
```
其中,`#inverse-btn` 是反选按钮的 ID,`table input[type='checkbox']` 是所有复选框的选择器。在点击反选按钮时,遍历所有复选框,将选中状态取反。这样就可以实现表格的反选功能了。
相关问题
表格反选jquery选中没选的
您可以使用以下代码来实现表格的反选功能,即将未选中的行选中,已选中的行取消选中:
```javascript
// 给反选按钮添加点击事件
$("#inverse-btn").click(function() {
// 遍历所有行
$("table tr").each(function() {
// 找到当前行的复选框
var checkbox = $(this).find("input[type='checkbox']");
// 如果复选框未选中,则选中;否则取消选中
if (!checkbox.prop("checked")) {
checkbox.prop("checked", true);
} else {
checkbox.prop("checked", false);
}
});
});
```
其中,`#inverse-btn` 是反选按钮的 ID,`table tr` 是所有行的选择器。在点击反选按钮时,遍历所有行,找到当前行的复选框,如果复选框未选中,则选中;否则取消选中。这样就可以实现表格的反选功能,将未选中的行选中,已选中的行取消选中。
jq表格反选功能代码实实现
表格反选功能是指当用户点击“反选”按钮时,会将表格中未选中的行变为选中状态,已选中的行变为未选中状态。下面是一个使用 jQuery 实现表格反选功能的示例代码:
HTML代码:
```html
<table id="myTable">
<thead>
<tr>
<th><input type="checkbox" id="checkAll"></th>
<th>Name</th>
<th>Age</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" name="select"></td>
<td>John</td>
<td>30</td>
<td>USA</td>
</tr>
<tr>
<td><input type="checkbox" name="select"></td>
<td>Jane</td>
<td>25</td>
<td>Canada</td>
</tr>
<tr>
<td><input type="checkbox" name="select"></td>
<td>Tom</td>
<td>40</td>
<td>UK</td>
</tr>
</tbody>
</table>
<button id="invert">Invert Selection</button>
```
JavaScript代码:
```javascript
$(function() {
// 全选/全不选
$("#checkAll").click(function() {
$("input[name='select']").prop("checked", $(this).prop("checked"));
});
// 反选
$("#invert").click(function() {
$("input[name='select']").each(function() {
$(this).prop("checked", !$(this).prop("checked"));
});
});
});
```
这段代码先给全选复选框 `#checkAll` 绑定了一个点击事件,当点击全选复选框时,会将所有的行复选框的选中状态与全选复选框的选中状态同步。然后给“反选”按钮 `#invert` 绑定了一个点击事件,当点击“反选”按钮时,会遍历所有的行复选框,将它们的选中状态取反。这样就实现了表格反选功能。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)