
在web开发中,表格(<table>)是展示结构化数据的常用方式。当表格的某个单元格(<td>)中包含交互式元素,例如下拉选择框(<select>),并且我们需要在用户操作该元素时,不仅获取其自身的值,还要获取同一行中其他单元格的关联数据时,就需要巧妙地运用dom遍历技术。
假设我们有一个HTML表格,其中每一行代表一个主机,包含主机名、用户和漏洞信息。漏洞信息在一个下拉选择框中展示,用户选择一个漏洞后,我们需要将所选漏洞的值与该行对应的主机名一同发送到后端进行处理。
以下是我们的HTML表格结构示例:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table">
<thead>
<tr>
<th scope="col">Hostname</th>
<th scope="col">User</th>
<th scope="col">Vulnerabilities</th>
</tr>
</thead>
<tbody>
<tr>
<td class="Rechnernummer">
<a href="javascript:void(0)" class="link-offset-2 link-offset-3-hover link-underline link-underline-opacity-0 link-underline-opacity-75-hover" onclick="clientInfo(event);">Host001</a>
</td>
<td class="User">User001</td>
<td class="Vulnerabilites">
<select class="form-select" size="4">
<option value="2705">Security Vulnerability CVE-2018-1285 for log4net</option>
<option value="73562">Security Vulnerability CVE-2022-39427 in VirtualBox</option>
</select>
</td>
</tr>
<tr>
<td class="Rechnernummer">
<a href="javascript:void(0)" class="link-offset-2 link-offset-3-hover link-underline link-underline-opacity-0 link-underline-opacity-75-hover" onclick="clientInfo(event);">Host002</a>
</td>
<td class="User">User002</td>
<td class="Vulnerabilites">
<select class="form-select" size="4">
<option value="68069">Security Vulnerability CVE-2021-34803 for TeamViewer</option>
<option value="74465">Security Vulnerability CVE-2023-21899 in VirtualBox</option>
</select>
</td>
</tr>
</tbody>
</table>用户在选择框中进行选择时,我们可以通过事件监听轻松获取所选选项的值:
$(function () {
$(".form-select").change(function (event) {
const selectedOptionValue = this.value; // 获取选中的option值
alert("选中的漏洞值: " + selectedOptionValue);
// ... 如何获取同行的主机名?
});
});然而,挑战在于如何从当前触发事件的<select>元素,定位到同一行中具有特定类名(例如Rechnernummer)的<td>元素,并提取其中的主机名。
jQuery提供了一系列强大的DOM遍历方法,可以帮助我们高效地在DOM树中移动。解决此类问题的关键在于利用closest()和find()这两个方法。
结合这两个方法,我们可以实现从<select>元素开始,先向上找到其所在的行(<tr>),然后在这行内部向下查找包含主机名的<td>元素。
以下是实现这一逻辑的JavaScript代码:
$(function () {
$(".form-select").change(function (event) {
const selectedOptionValue = this.value; // 获取选中的option值
// 1. 从当前 <select> 元素向上查找最近的 <tr> 祖先元素
const $currentRow = $(this).closest('tr');
// 2. 在找到的 <tr> 元素内部,向下查找具有 class="Rechnernummer" 的 <td>,
// 然后在其内部查找 <a> 标签,并获取其文本内容
const hostname = $currentRow.find('td.Rechnernummer a').text();
alert("选中的漏洞值: " + selectedOptionValue + "\n对应主机名: " + hostname);
// 可选:重置选择,如果业务逻辑需要
// for(var i = 0; i < this.options.length; i++){
// this.options[i].selected = false;
// }
});
});代码解析:
将HTML结构与改进后的JavaScript结合,形成一个完整的可运行示例:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery DOM 遍历获取表格数据</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
.form-select {
width: 100%;
padding: 5px;
border: 1px solid #ccc;
border-radius: 4px;
}
</style>
</head>
<body>
<h1>表格联动数据获取示例</h1>
<table class="table">
<thead>
<tr>
<th scope="col">Hostname</th>
<th scope="col">User</th>
<th scope="col">Vulnerabilities</th>
</tr>
</thead>
<tbody>
<tr>
<td class="Rechnernummer">
<a href="javascript:void(0)" onclick="clientInfo(event);">Host001</a>
</td>
<td class="User">User001</td>
<td class="Vulnerabilites">
<select class="form-select" size="4">
<option value="2705">Security Vulnerability CVE-2018-1285 for log4net</option>
<option value="73562">Security Vulnerability CVE-2022-39427 in VirtualBox</option>
</select>
</td>
</tr>
<tr>
<td class="Rechnernummer">
<a href="javascript:void(0)" onclick="clientInfo(event);">Host002</a>
</td>
<td class="User">User002</td>
<td class="Vulnerabilites">
<select class="form-select" size="4">
<option value="68069">Security Vulnerability CVE-2021-34803 for TeamViewer</option>
<option value="74465">Security Vulnerability CVE-2023-21899 in VirtualBox</option>
</select>
</td>
</tr>
</tbody>
</table>
<script>
$(function () {
$(".form-select").change(function (event) {
const selectedOptionValue = this.value; // 获取选中的option值
// 使用 .closest() 向上找到最近的 <tr> 元素
const $currentRow = $(this).closest('tr');
// 在当前行内部使用 .find() 向下查找具有 class="Rechnernummer" 的 <td>,
// 然后获取其内部 <a> 标签的文本内容
const hostname = $currentRow.find('td.Rechnernummer a').text();
alert("选中的漏洞值: " + selectedOptionValue + "\n对应主机名: " + hostname);
// 如果需要,可以在这里发起AJAX请求到Django后端
// 例如:
// $.ajax({
// url: '/your-django-endpoint/',
// method: 'POST',
// data: {
// vulnerability_id: selectedOptionValue,
// hostname: hostname,
// csrfmiddlewaretoken: '{{ csrf_token }}' // 如果是Django模板,需要CSRF token
// },
// success: function(response) {
// console.log('数据发送成功', response);
// },
// error: function(error) {
// console.error('数据发送失败', error);
// }
// });
});
});
// clientInfo 函数可能在其他地方定义,这里为了示例完整性保留
function clientInfo(event) {
console.log('Client info clicked for:', event.target.innerText);
}
</script>
</body>
</html>$(document).on('change', '.form-select', function() {
// ... 相同的逻辑 ...
});这将把事件监听器绑定到文档(或某个稳定的父元素),而不是每个.form-select元素,从而可以处理未来动态添加的元素。
通过jQuery的closest()和find()方法,我们可以非常灵活和高效地在复杂的HTML表格结构中进行DOM遍历,从而获取到不同但相关联的元素数据。这种技术模式在处理表格中交互式组件与行级数据的联动时非常实用,是前端开发中一项重要的技能。掌握这些DOM遍历技巧,将有助于您构建更强大、更响应式的Web应用。
以上就是jQuery DOM 遍历技巧:在表格中联动获取选择框值与同行列数据的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号