【发布时间】:2010-04-30 05:19:25
【问题描述】:
当用户在页面(基本上是一个大表)上输入搜索词时,我正在尝试为用户提交的搜索结果添加背景颜色。这是基于文本的搜索。我正在使用 jquery 在 TR 中显示/隐藏没有搜索词作为文本的表行,但理想情况下,我希望采取额外的步骤来获取搜索词(输入的值),并匹配任何其余(显示)行中的这些文本术语,并为单词添加黄色背景。我知道我的语法目前是错误的,只是不确定什么是正确的:)希望这很清楚......非常感谢任何帮助!
$("#searchsubmit").click(function () {
var searchexp = document.getElementById('searchbox').value;
$("table tr").hide();
$("table tr.header").show();
$('tr:contains('+ searchexp +')').show();
$(searchexp).css('background-color','yellow');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="searchform" method="get" action="#">
<input type="text" id="searchbox" />
<input type="submit" value="Search" id="searchsubmit" />
</form>
【问题讨论】: