In-page search using contains() to show/hide div content

三世轮回 提交于 2019-12-22 10:11:21

问题


I am trying to add a search functionality to my FAQ page and am absolutely stuck.

What I want is a text box where the user inputs a keyword(or words), that runs a jquery for the keyword and sets display:block for all the relevant answers.

What I have so far is this:

    <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>

回答1:


Try this

function searchFunction() {
          var searchTerm = document.searchBox.keyword.value;
          $(".searchable").each(function(){
              $(this).(":contains('"+searchTerm+"')").show(); 
           });

}



回答2:


Try this.

function searchFunction() {
    $(".searchable")
        .hide()
        .filter(":contains('" + $("input[name='keyword']").val() + "')")
        .show();
}



回答3:


Change .addStyle("display:block") to .show()



来源:https://stackoverflow.com/questions/6709718/in-page-search-using-contains-to-show-hide-div-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!