jQuery使用上下方向键遍历表格搜索结果并高亮选中行问题求助
解决方案
核心问题是原有代码选中了所有表格行(包含搜索后隐藏的行),只需要将选中行的逻辑修改为仅筛选可见行即可,同时修复了回车键重复绑定的冗余问题。
修改要点
- 行筛选逻辑从
$table.find('tr')改为$table.find('tbody tr:visible'),排除表头和搜索隐藏的行 - 将回车键监听事件移到方向键判断逻辑外,避免重复绑定
- 新增方向键默认行为阻止,避免按上下键时输入框光标前后移动
- 新增无选中行时的默认选中逻辑,第一次按方向键直接选中第一条/最后一条可见结果
- 搜索后自动清空原有高亮,避免隐藏行残留高亮状态
完整可运行代码
CSS
.highlight_row { background-color: red; }
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container"> <input type="text" onkeyup="myFunction()" class="form-control form-rounded rounded-pill" placeholder="Text input" id="searchbar"> <table id="myTable" class="table child-div" > <thead> <tr> <th scope="col">#</th> <th scope="col">First</th> <th scope="col">Last</th> <th scope="col">Handle</th> </tr> </thead> <tbody> <tr> <th scope="row">1</th> <td>Mark</td> <td>Otto</td> <td>@mdo</td> </tr> <tr> <th scope="row">2</th> <td>Jacob</td> <td>Thornton</td> <td>@fat</td> </tr> <tr> <th scope="row">3</th> <td>Larry</td> <td>the Bird</td> <td>@twitter</td> </tr> <tr> <th scope="row">3</th> <td>farry</td> <td>the Bird</td> <td>@twitter</td> </tr> <tr> <th scope="row">3</th> <td>marry</td> <td>the Bird</td> <td>@twitter</td> </tr> <tr> <th scope="row">3</th> <td>narry</td> <td>the Bird</td> <td>@twitter</td> </tr> </tbody> </table> </div>
JS
$(function() { const UP = 38; const DOWN = 40; const ARROWS = [UP, DOWN]; const HIGHLIGHT = 'highlight_row'; let $table = $('.child-div'); let selectedRow = -1; // 回车键监听单独绑定,避免重复注册 $(document).on('keypress',function(e) { if(e.which == 13) { let $visibleRows = $table.find('tbody tr:visible'); if(selectedRow >=0 && selectedRow < $visibleRows.length){ alert($visibleRows[selectedRow].cells[1].innerText); } } }); $('#searchbar').on('input keydown', function(e) { $table = $('.child-div'); // 仅筛选可见的tbody行 let $visibleRows = $table.find('tbody tr:visible'); let key = e.which; if (ARROWS.includes(key)) { // 阻止方向键触发输入框光标移动 e.preventDefault(); selectedRow = -1; $visibleRows.each(function(i, row) { if ($(row).hasClass(HIGHLIGHT)) { selectedRow = i; } }); // 无选中行时默认选中第一行/最后一行 if(selectedRow === -1){ selectedRow = key === DOWN ? 0 : $visibleRows.length -1; }else{ if (key == UP && selectedRow > 0) { selectedRow--; } else if (key == DOWN && selectedRow < $visibleRows.length - 1) { selectedRow++; } } $visibleRows.removeClass(HIGHLIGHT); $visibleRows.eq(selectedRow).addClass(HIGHLIGHT); } }); }); function myFunction() { var input, filter, table, tr, td, i, txtValue; input = document.getElementById("searchbar"); filter = input.value.toUpperCase(); table = document.getElementById("myTable"); tr = table.getElementsByTagName("tr"); // 搜索后清空原有高亮 $('.highlight_row').removeClass('highlight_row'); for (i = 0; i < tr.length; i++) { td = tr[i].getElementsByTagName("td")[0]; if (td) { txtValue = td.textContent || td.innerText; if (txtValue.toUpperCase().indexOf(filter) > -1) { tr[i].style.display = ""; } else { tr[i].style.display = "none"; } } } }
内容的提问来源于stack exchange,提问作者steve jhone
相关产品推荐
相关产品推荐

