【问题标题】:Accordion JQuery Search手风琴 JQuery 搜索
【发布时间】:2018-02-13 11:36:36
【问题描述】:

我在我正在构建的网站上设置了一个手风琴,但是我一直在尝试实现搜索功能。 可能在您键入时,根据搜索隐藏和显示每个手风琴。

这是我的两个卡片的 HTML 代码,它们是手风琴的一部分:

<div class="search-container">
            <input class="input-medium search-query" id="searcher" placeholder="Search">
            <button class="btn">Search</button>
    </div>
    <br>
    <div id="accordion" class="accordion">
        <div class="card m-b-0">
            <div class="card-header collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
                <a class="card-title">
                    Why should I use Imperial Claims Consultants?
                </a>
            </div>
            <div id="collapseOne" class="card-block collapse" style="padding: 20px;">
                <p>
                    Imperial Claims are industry experts in dealing with commercial, industrial, homeowners and landlords insurance claims and we have, and continue to provide our clients with a truly personal and bespoke service. With over 80 years combined in-house claims handling experience there isn’t any type of disaster that we haven’t already managed. From small claims to the largest of claims, we can truly assist.
                </p>
                <p>
                    Loss assessors are typically professional companies and individuals that will negotiate on the policyholder’s behalf to progress a claim to resolution. It could be said that they offer a claims management service for the policyholder, meaning the policyholder doesn’t have to spend time dealing with the insurer – the loss assessor will typically do this.
                </p>
                <p>
                    A Loss Assessor, like Imperial Claims Consultants, will look at the damage, assess the costs involved and they may then take the lead in all discussions on the claim with the insurers or loss adjusters concerned. Loss Assessors are employed by the policyholder, they are acting exclusively to protect the policyholders best interests.
                </p>
                <p>
                    Imperial Claims Consultants are a Financial Conduct Authority registered firm. We are authorised to assist clients with their insurance claims. We work for the policyholder only, never the insurer. Our service will not cost the insured anything provided they use our panel of contractors.
                </p>
                <p>
                    We help clients who have suffered from the insurance covered perils relating to building and/or contents such as:
                </p>
                <ul>
                    <li><span> - </span>Fire</li>
                    <li><span> - </span>Flood</li>
                    <li><span> - </span>Explosion</li>
                    <li><span> - </span>Storm</li>
                    <li><span> - </span>Impact Damage</li>
                    <li><span> - </span>Subsidence</li>
                    <li><span> - </span>Theft</li>
                    <li><span> - </span>Burglary</li>
                    <li><span> - </span>Contents</li>
                    <li><span> - </span>Business Loss</li>
                    <li><span> - </span>Liability Claims</li>
                    <li><span> - </span>Professional Indemnity</li>
                    <li><span> - </span>Loss Of Licence</li>
                </ul>
            </div>
        </div>
        <br>
        <div class="card m-b-0">
            <div class="card-header collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
                <a class="card-title">
                    Which insurance claims can Imperial Claims Consultants help me with?
                </a>
            </div>
            <div id="collapseTwo" class="card-block collapse" style="padding: 20px;">
                <p>
                    We help clients who have suffered from the insurance covered perils relating to building and/or contents such as:
                </p>
                <ul>
                    <li><span> - </span>Fire</li>
                    <li><span> - </span>Flood</li>
                    <li><span> - </span>Explosion</li>
                    <li><span> - </span>Storm</li>
                    <li><span> - </span>Impact Damage</li>
                    <li><span> - </span>Subsidence</li>
                    <li><span> - </span>Theft</li>
                    <li><span> - </span>Burglary</li>
                    <li><span> - </span>Contents</li>
                    <li><span> - </span>Business Loss</li>
                    <li><span> - </span>Liability Claims</li>
                    <li><span> - </span>Professional Indemnity</li>
                    <li><span> - </span>Loss Of Licence</li>
                </ul>
                <p>
                    So, before you contact the insurers, contact Imperial Claims Consultants for a very personal and bespoke service.
                </p>
                <p>
                    Don’t forget, If our sister company, Imperial Maintenance and/or its sub-contractors are used for managing the entire reinstatement works process, then you truly will not have a single penny to pay at all as our fee would be paid by our sister company.
                </p>
            </div>
        </div>

这也包括我的搜索栏。

但是,我需要根据文本搜索显示卡片。 我尝试了以下方法,但即使使用按钮也无法正常工作:

 $("#searcher").on("keyup click input", function () {
    var val = $(this).val();
    if(val.length) {
        $(".accordion .card.m-b-0").hide().filter(function () {
            return $('input', this).val().toLowerCase().indexOf(val.toLowerCase()) !== -1;
        }).show();
    }
    else {
        $(".accordion .card.m-b-0").show();
    }
});

有人能指出我正确的方向吗?

【问题讨论】:

  • 你能贴个小提琴吗..?
  • $('input', this).val() 这段代码的意思是从".accordion .card.m-b-0" 内部的输入中获取值,我认为您需要改用$('.card-title', this).text()
  • @Mohamed-Yousef 你从哪里得到 .card-title?
  • @NikhilGhuse jsfiddle.net/j9zhn3pw
  • 什么问题?当然我是从你的代码得到的。好吧顺便说一句,你可以使用return $('.card-title', this).text().toLowerCase().indexOf(val.toLowerCase()) &gt; -1;jsfiddle.net/j9zhn3pw/1

标签: javascript jquery html accordion


【解决方案1】:

$(document).ready(function(){
  $("#searcher").on("keypress click input", function () {
      var val = $(this).val();
      if(val.length) {
          $(".accordion .card.m-b-0").hide().filter(function () {
              return $('.card-title', this).text().toLowerCase().indexOf(val.toLowerCase()) > -1;
          }).show();
      }
      else {
          $(".accordion .card.m-b-0").show();
      }
  });
  
  
  //for test
  //$("#searcher").val('us').keyup();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="search-container">
            <input class="input-medium search-query" id="searcher" placeholder="Search">
            <button class="btn">Search</button>
    </div>
    <br>
    <div id="accordion" class="accordion">
        <div class="card m-b-0">
            <div class="card-header collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
                <a class="card-title">
                    Why should I use Imperial Claims Consultants?
                </a>
            </div>
            <div id="collapseOne" class="card-block collapse" style="padding: 20px;">
                <p>
                      We help them clients who have suffered from the insurance covered perils relating to building and/or
                
                </p>
                <p>
                    We help clients  who have suffered from the insurance covered perils relating to building and/or contents such as:
                </p>               
            </div>
        </div>
        <br>
        <div class="card m-b-0">
            <div class="card-header collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
                <a class="card-title">
                    Which insurance claims can Imperial Claims Consultants help me with?
                </a>
            </div>
            <div id="collapseTwo" class="card-block collapse" style="padding: 20px;">
                <p>
                    We help clients who have suffered from the insurance covered perils relating to building and/or contents such as:
                </p>               
                <p>
                    So, before you contact the insurers, contact Imperial Claims Consultants for a very personal and bespoke service.
                </p>
                
            </div>
        </div>

根据您的要求,我已经更新了答案

$(document).ready(function(){
  $("#searcher").on("keypress click input", function () {
      var val = $(this).val();
      if(val.length) {
          $(".accordion .card.m-b-0 .collapsed").hide().filter(function () {
              return $('.card-block', this).text().toLowerCase().indexOf(val.toLowerCase()) > -1;
          }).show();
      }
      else {
          $(".accordion .card.m-b-0 .collapsed").show();
      }
  });
  
  
  //for test
  //$("#searcher").val('us').keyup();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="search-container">
            <input class="input-medium search-query" id="searcher" placeholder="Search">
            <button class="btn">Search</button>
    </div>
    <br>
    <div id="accordion" class="accordion">
        <div class="card m-b-0">
            <div class="card-header collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
                <a class="card-title">
                    Why should I use Imperial Claims Consultants?
                </a>
            </div>
            <div id="collapseOne" class="card-block collapse" style="padding: 20px;">
                <p>
                      We help them clients who have suffered from the insurance covered perils relating to building and/or
                
                </p>
                <p>
                    We help clients  who have suffered from the insurance covered perils relating to building and/or contents such as:
                </p>               
            </div>
        </div>
        <br>
        <div class="card m-b-0">
            <div class="card-header collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
                <a class="card-title">
                    Which insurance claims can Imperial Claims Consultants help me with?
                </a>
            </div>
            <div id="collapseTwo" class="card-block collapse" style="padding: 20px;">
                <p>
                    We help clients who have suffered from the insurance covered perils relating to building and/or contents such as:
                </p>               
                <p>
                    So, before you contact the insurers, contact Imperial Claims Consultants for a very personal and bespoke service.
                </p>
                
            </div>
        </div>

【讨论】:

  • 在你的sn-p上工作得很好。但是,我的页面一定有问题。
  • 是的,我知道,我在上面放了一个要点……在第一篇文章 cmets 中。你能看一下,看看你能不能明白为什么?
  • 您能告诉我如何根据手风琴的内容进行搜索吗?而不是卡片标题。
  • 更新了我的答案,第一个 sn-p 是 card-title,第二个是 Content Of Accordian
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
  • 2018-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-13
  • 1970-01-01
相关资源
最近更新 更多