简体   繁体   中英

Filter with parents selector

help me please. I need to add a filter to the paint catalog.

I have a code like this on the page:

<div class="container">
    <div class="row">
        <div class="col-12">
            <input id="myInputDiv" type="text">
            <script>
                (function ($) {
                    $('#myInputDiv').on('keyup', function () {
                        var value = $(this).val().toLowerCase();
                        $('#myDIV *').filter(function () {
                            $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1);
                        });
                    });
                }(jQuery));
            </script>
        </div>
    </div>
    <div class="row" id="myDIV">
        <div class="col-4 col-sm-4 col-md-2 color-teaser">
            <div class="views-fields">
                <div class="views-field views-field-field-c-rgb-id">
                    <div class="color-teaser-display" style="background-color: #F6F3E6; color: #F6F3E6" role="img" alt="#F6F3E6"></div>
                </div>
                <div class="views-field views-field-title"> <span class="field-content">F300</span>
                </div>
            </div>
        </div>
        <div class="col-4 col-sm-4 col-md-2 color-teaser">
            <div class="views-fields">
                <div class="views-field views-field-field-c-rgb-id">
                    <div class="color-teaser-display" style="background-color: #F9EFD8; color: #F9EFD8" role="img" alt="#F9EFD8"></div>
                </div>
                <div class="views-field views-field-title"> <span class="field-content">G300</span>
                </div>
            </div>
        </div>
    </div>
</div>

As a result of the script I get this:

<div class="views-field views-field-title"> 
    <span class="field-content">F300</span>
</div>

But I need this:

<div class="col-4 col-sm-4 col-md-2 color-teaser c-id-24958 views-row">
    <div class="views-fields">
        <div class="views-field views-field-field-c-rgb-id">
            <div class="color-teaser-display" style="background-color: #F6F3E6; color: #F6F3E6" role="img" alt="#F6F3E6"></div>
        </div>
        <div class="views-field views-field-title"> <span class="field-content">F300</span>
        </div>
    </div>
</div>

How can I change the script to get the desired result?

instead of $('#myDIV *') use $('#myDIV > div') so you are only filtering the dive with col-4 in it

 (function ($) { $('#myInputDiv').on('keyup', function () { var value = $(this).val().toLowerCase(); $('#myDIV > div').filter(function () { $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1); }); }); }(jQuery));
 .color-teaser{ background: red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container"> <div class="row"> <div class="col-12"> <input id="myInputDiv" type="text"> </div> </div> <div class="row" id="myDIV"> <div class="col-4 col-sm-4 col-md-2 color-teaser"> <div class="views-fields"> <div class="views-field views-field-field-c-rgb-id"> <div class="color-teaser-display" style="background-color: #F6F3E6; color: #F6F3E6" role="img" alt="#F6F3E6"></div> </div> <div class="views-field views-field-title"> <span class="field-content">F300</span> </div> </div> </div> <div class="col-4 col-sm-4 col-md-2 color-teaser"> <div class="views-fields"> <div class="views-field views-field-field-c-rgb-id"> <div class="color-teaser-display" style="background-color: #F9EFD8; color: #F9EFD8" role="img" alt="#F9EFD8"></div> </div> <div class="views-field views-field-title"> <span class="field-content">G300</span> </div> </div> </div> </div> </div>

You have to filter on the children elements of myDIV :

 (function ($) { $('#myInputDiv').on('keyup', function () { var value = $(this).val().toLowerCase(); $('#myDIV').children().filter(function () { $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1); }); }); }(jQuery));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container"> <div class="row"> <div class="col-12"> <input id="myInputDiv" type="text"> </div> </div> <div class="row" id="myDIV"> <div class="col-4 col-sm-4 col-md-2 color-teaser"> <div class="views-fields"> <div class="views-field views-field-field-c-rgb-id"> <div class="color-teaser-display" style="background-color: #F6F3E6; color: #F6F3E6" role="img" alt="#F6F3E6"></div> </div> <div class="views-field views-field-title"> <span class="field-content">F300</span> </div> </div> </div> <div class="col-4 col-sm-4 col-md-2 color-teaser"> <div class="views-fields"> <div class="views-field views-field-field-c-rgb-id"> <div class="color-teaser-display" style="background-color: #F9EFD8; color: #F9EFD8" role="img" alt="#F9EFD8"></div> </div> <div class="views-field views-field-title"> <span class="field-content">G300</span> </div> </div> </div> </div> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM