jQuery隐藏复选框多个值

时间:2021-09-07 09:48:38

I have 7 checkboxes with all a different ID. Those ID's correspondent with div's containing an data-methode in it. Clicking on a checkbox will hide all div's except for that one with the corresponding ID.

我有7个复选框,所有ID都不同。那些ID与div的对应,其中包含数据方法。单击复选框将隐藏除具有相应ID的那个div之外的所有div。

At this moment that works! But when I select multiple checkboxes all the divs disappear.

这一刻有效!但是当我选择多个复选框时,所有div都会消失。

This is my javascript:

这是我的javascript:

<script type="text/javascript"> 
jQuery('.filter_methode input').change(function()
    {
        jQuery('.featured-item-container').show();
        jQuery('input').each(function()
            {
            var Checked;
            if(Checked = jQuery(this).attr('checked'))
                {
                    jQuery(".featured-item-container:not([data-methode='" + this.id + "'])").hide();
                };
        }); 
    });
</script>

I wonder how I can optimize this script so it listens to all selected ID's.

我想知道如何优化这个脚本,以便它可以监听所有选定的ID。

Edit Here is the HTML:

编辑这是HTML:

<div class="filter_methode">
<label><input type="checkbox" name="methode_1"  id="methode_1" />
Methode 1</label>

<label><input type="checkbox" name="methode_2" id="methode_2"  />
Methode 2</label>

<label><input type="checkbox" name="methode_3" id="methode_3"  />
Methode 3</label>

<label><input type="checkbox" name="methode_4" id="methode_4" />
Methode 4</label>

<label><input type="checkbox" name="methode_5" id="methode_5" />
Methode 5</label>


<label><input type="checkbox" name="methode_6" id="methode_6" />
Methode 6</label>


<label><input type="checkbox" name="methode_7" id="methode_7" />
Methode 7</label>

<label><input type="checkbox" name="methode_8" id="methode_8" />
Methode 8</label>

<div class="featured-container col-md-12">
    <div class="row">
        <div class="featured-item-container" data-methode="methode_1">
            Some text and image in here
        </div>
        <div class="featured-item-container" data-methode="methode_1">
            Some text and image in here
        </div>
        <div class="featured-item-container" data-methode="methode_1">
            Some text and image in here
        </div>
        <div class="featured-item-container" data-methode="methode_3">
            Some text and image in here
        </div>
        <div class="featured-item-container" data-methode="methode_7">
            Some text and image in here
        </div>
        <div class="featured-item-container" data-methode="methode_8">
            Some text and image in here
        </div>
        <div class="featured-item-container" data-methode="methode_2">
            Some text and image in here
        </div>
        <div class="featured-item-container" data-methode="methode_4">
            Some text and image in here
        </div>
        <div class="featured-item-container" data-methode="methode_8">
            Some text and image in here
        </div>
        <div class="featured-item-container" data-methode="methode_5">
            Some text and image in here
        </div>
        <div class="featured-item-container" data-methode="methode_6">
            Some text and image in here
        </div>
        <div class="featured-item-container" data-methode="methode_5">
            Some text and image in here
        </div>
        <div class="featured-item-container" data-methode="methode_1">
            Some text and image in here
        </div>
        <div class="featured-item-container" data-methode="methode_2">
            Some text and image in here
        </div>
    </div>
</div>

So when I click checkbox with ID methode_1 it does show me only the divs with data-methode="methode_1", but when I also click on checkbox methode_2 all divs disappear instead of showing me the divs with methode_1 and methode_2

因此,当我点击带有ID methode_1的复选框时,它确实只向我显示带有data-methode =“methode_1”的div,但是当我也点击复选框methode_2时,所有div都会消失而不是向我显示带有methode_1和methode_2的div

1 个解决方案

#1


2  

Now that I have the code here is the solution:

现在我在这里有代码是解决方案:

jQuery('.filter_methode input').change(function()
    {
        jQuery('.featured-item-container').hide();
        jQuery('input').each(function(k,v)
            { 
            var Checked = $(v).prop( "checked" );
            if(Checked)
                { 
                    jQuery(".featured-item-container[data-methode='" + v.id + "']").show();
                };
        }); 
        if(jQuery('input:checked').length == 0) {
            jQuery('.featured-item-container').show();
        }
    });

there is no "this" in an anonymous function. You can access the object with the second parameter.

匿名函数中没有“this”。您可以使用第二个参数访问该对象。

see here: https://jsfiddle.net/92u6u72L/3/

看到这里:https://jsfiddle.net/92u6u72L/3/

#1


2  

Now that I have the code here is the solution:

现在我在这里有代码是解决方案:

jQuery('.filter_methode input').change(function()
    {
        jQuery('.featured-item-container').hide();
        jQuery('input').each(function(k,v)
            { 
            var Checked = $(v).prop( "checked" );
            if(Checked)
                { 
                    jQuery(".featured-item-container[data-methode='" + v.id + "']").show();
                };
        }); 
        if(jQuery('input:checked').length == 0) {
            jQuery('.featured-item-container').show();
        }
    });

there is no "this" in an anonymous function. You can access the object with the second parameter.

匿名函数中没有“this”。您可以使用第二个参数访问该对象。

see here: https://jsfiddle.net/92u6u72L/3/

看到这里:https://jsfiddle.net/92u6u72L/3/