
在Web开发中,经常会遇到需要获取表格中特定行数据的情况,例如排除当前操作行,获取其他行的信息。本文将介绍如何使用JavaScript和jQuery来实现这一功能。
核心思路如下:
以下是实现上述功能的代码示例:
<!DOCTYPE html>
<html>
<head>
<title>获取HTML表格中未选中行的值</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<table id="all_departments">
<thead>
<th><button>click</button></th>
<th>Departments</th>
<th>Creation Date</th>
<th>Name</th>
</thead>
<tbody class="bl">
<tr>
<td><button>click</button></td>
<td>Management</td>
<td>2-3-2016</td>
<td>client x</td>
</tr>
<tr>
<td><button>click</button></td>
<td>Sales</td>
<td>25-6-2019</td>
<td>client y</td>
</tr>
</tbody>
</table>
<script>
$(document).ready(function() {
$('#all_departments button').click(function() {
// 获取当前按钮所在的 tr 元素
var currentRow = $(this).closest('tr');
// 获取当前 tr 元素的所有兄弟节点(即其他 tr 元素)
var siblings = currentRow.siblings();
// 存储结果的数组
var result = [];
// 遍历兄弟节点,提取数据
siblings.each(function() {
$(this).find('td').each(function(index) {
//跳过第一个button列
if(index != 0){
result.push($(this).text());
}
});
});
// 输出结果
console.log(result.join(','));
});
});
</script>
</body>
</html>代码解释:
立即学习“前端免费学习笔记(深入)”;
本文介绍了如何使用JavaScript和jQuery获取HTML表格中未选中行的值。通过监听按钮点击事件,获取当前行,然后获取其兄弟节点,并提取这些节点中的数据,最终将结果输出。 这种方法可以方便地获取表格中特定行的数据,为Web开发提供了便利。
以上就是获取HTML表格中未选中行的值:jQuery实现教程的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号