我正在尝试在我的常见问题解答页面添加搜索功能,但我绝对卡住了。
我想要的是一个文本框,用户输入一个关键字(或多个单词),为关键字运行jquery,并为所有相关答案设置display:block。
到目前为止我所拥有的是:
<form name="searchBox">
Keyword(s): <input type="text" name="keyword" />
<input type="button" value="Search" onClick="searchFunction()" />
</form>
<div class="searchable" style="display:none">
This is the first software question and answer.</div>
<div class="searchable" style="display:none">
This is the first hardware question and answer.</div>
<script type="text/javascript">
function searchFunction() {
var searchTerm = document.searchBox.keyword.value;
$(" :contains('"+searchTerm+"')").addStyle("display:block"); }
</script>
答案 0 :(得分:7)
试试这个
function searchFunction() {
var searchTerm = document.searchBox.keyword.value;
$(".searchable").each(function(){
$(this).(":contains('"+searchTerm+"')").show();
});
}
答案 1 :(得分:4)
试试这个。
function searchFunction() {
$(".searchable")
.hide()
.filter(":contains('" + $("input[name='keyword']").val() + "')")
.show();
}
答案 2 :(得分:0)
将.addStyle(“display:block”)更改为.show()