douhu1990
2017-07-19 11:55
浏览 30

没有匹配jQuery的搜索栏警报

I made simple searchbar for my app. Right now it searching the users by data attribute but when it doesn't find any match it display nothing. I want it to show error message when there is no match. I made if statement but it only display when I type something into input and delete it. How to display error message immediately when searchbar doesn't find any user.

JS

       $(document).ready(function () {
//        Pokaż/Ukryj wyszukiwarkę
        $('.show-hide-search-bar').on('click', function () {
            if ($('.searcher-section').is(":visible")) {
                $('.searcher-section').hide("slide");
                $('.show-hide-search-bar').text('Pokaż Wyszukiwarkę');
            } else {
                $('.searcher-section').show("slide");
                $('.show-hide-search-bar').text('Ukryj Wyszukiwarkę');
            }
        });


//        Zmiana tekstu w zależności od pojawienia się searchera
        if ($('#agents').length > 0) {
            $('#label-searchbar').html('Imię, Nazwisko, Adres email');
            $('#input-searchbar').attr('placeholder', 'Podaj imię, nazwisko lub adres email');
        } else if ($('#company').length > 0) {
            $('#label-searchbar').html('Nazwa biura');
            $('#input-searchbar').attr('placeholder', 'Podaj nazwę biura');
        }


//Dynamiczne wyszukiwanie .card dla agentów | .clearfix dla listy

        $('.searcher-input').keyup(function (event) {
            $('.null-data').hide();
            if ($(this).val()) {
                var input = $(this).val();
                var trimmedInput = input.trim();
                var terms = input.split(/\W+/g);
                $(".card").hide();

                $(".clearfix.alt").hide();
                $(".card[data-agent*='" + trimmedInput + "' i]").show();

                $(".clearfix[data-name*='" + trimmedInput + "' i]").show();

//                $(".card[data-lastname*='" + trimmedInput + "']").show();
//                $(".card[data-lastname*='" + trimmedInput + "'][data-firstname*='" + trimmedInput + "']").show();
//                $(".card[data-email*='" + trimmedInput + "']").show();

                $(".col-xs-12").css("min-height", "0");
                $(".col-md-4").css("min-height", "0");
                $(".col-sm-5").css("min-height", "0");
                if ($('.card').is(':visible').get(0)) {
                    $('.null-data').show(function () {
                        $(this).effect("shake");
                    });
                }
                if (!$('.clearfix').is(':visible').get(0)) {
                    $('.null-data').show(function () {
                        $(this).effect("shake");
                    });
                }
            } else {
                $(".clearfix.alt").show();
                $(".card").show();
                $('.null-data').show(function () {
                    $(this).effect("shake");
                });
            }

        });


    });

HTML

<div class="container">
    <div class="row">
        <div class="col-xs-12 col-md-12">

            <div class="searcher-section" style="display: none">
                <div class="show-hide-searcher">
                    <div class="input-section">

                        <div class="label-input-searcher">
                            <label for="" class="searcher-label" id="label-searchbar"></label>
                            <input type="text" placeholder=""
                                   class="searcher-input form-control" id="input-searchbar"/>
                            <div class="null-data" style="display: none;">Wprowadź poprawne dane</div>

                        </div>

                    </div>

                </div>
            </div>
        </div>
        <div class="show-hide-section">
            <button class="btn btn-success show-hide-search-bar" id="searchbar-button">Pokaż wyszukiwarkę</button>
        </div>
    </div>
</div>

<div class="container">
    <div class="row">
        <h3 class="title" id="agents">Doradcy</h3>

        <div class="cards">
            @foreach($agents as $agent)
                <div class="col-xs-12 col-sm-5 col-md-4">

                    <div class="card" data-agent="{{$agent->firstname}} {{$agent->lastname}} {{$agent->email}}">
                        <figure>
                            <div class="img-ref">
                                <a href=""
                                   class="">
                                    @if(isset($agent->has_avatar) && $agent->has_avatar !== 0)
                                    <div style="background: url(''); background-size: cover;" class="photo"></div>
                                    @else
                                        <div style="background: url(''); background-size: cover;"
                                             class="photo"></div>
                                    @endif
                                </a>
                            </div>
                            <ul>
                                <li>
                                    <a href=""
                                       class="teamLink agent-color">
                                        <h3 class="agent-name">{{$agent->firstname}} {{$agent->lastname}}</h3>                                    </a>
                                </li>
                            </ul>
                            @if(isset($agent->company_name))
                            <div class="teams-summary">
                                {{$agent->company_name}}
                            </div>
                            @endif
                            <div class="contact-position">
                                {{--telefon kontaktowy--}}
                                <div class="mobile-info card-contact-info">
                                    {{$agent->phone}}
                                </div>
                                {{--adres mailowy--}}
                                <div class="email-info card-contact-info">
                                    {{$agent->email}}
                                </div>
                            </div>
                        </figure>
                    </div>
                </div>
            @endforeach
        </div>
        {{----}}
    </div>
</div>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyue1926 2017-07-19 12:26
    已采纳

    check element length

    ........
    $(".clearfix.alt").hide();
    
    if($(".card[data-agent*='" + trimmedInput + "' i]").length == 0) {
      alert("no result");
      return;
    }
    
    $(".card[data-agent*='" + trimmedInput + "' i]").show();
    ....
    
    打赏 评论

相关推荐 更多相似问题