【问题标题】:Show only the item with the classname equal to the multiple selected checkbox values jQuery仅显示类名等于多个选中复选框值jQuery的项目
【发布时间】:2017-06-27 07:04:33
【问题描述】:

我想创建一个过滤系统,其中只显示类名等于所选复选框值的文章。 用户可以选择多个 cb,结果应该只是那些文章。

I know how to show the article with the same selected checkbox value, but when multiple are selected the article that doesn't contain one of those cb value is still shown, so I tried to make it smarter. 到目前为止还没有运气。

编辑链接:http://strosslenet.nl/demo/ -- 更改了链接,以便其他人也可以查看。

jQuery 代码:

$("#filters :checkbox").click(function() { // if click on a checkbox
    $(".widget-lijst article").hide(); // hide all the items first

    var valCB = $(this).val() // value of the checkbox;

    $("#filters :checkbox:checked").each(function() {
        var valObj = $("." + $(this).val()); // value of the object
        var $allClasses = valObj.attr('class').split(' '); // splits every class in a object

        console.log(valObj);

        for(var i=0; i < $allClasses.length; i++) {

            if($allClasses == valCB) {

                console.log('true');

            } else {

                console.log('false');
            }
        }
    });
});

HTML 代码:

<ul id="filters">
  <li>Type
    <ul>
      <li><input type="checkbox" value="category-sc" id="sc" /><label for="category-sc">Site content</label></li>
      <li><input type="checkbox" value="category-va" id="va" /><label for="category-va">Visitor accelerator</label></li>
      <li><input type="checkbox" value="category-ad" id="ad" /><label for="category-ad">Ad only</label></li>
    </ul>
  </li>
</ul> 

这些项目是由 Wordpress 帖子创建的。
Ajaxshowtime 具有类“类别-sc”
Babes Panorama 具有“类别-va”类
博蒙德两者都有

理想的情况是,当同时选择访问者加速器和网站内容时,只有 Beaumonde 会显示。

【问题讨论】:

    标签: javascript jquery html wordpress checkbox


    【解决方案1】:

    您可以尝试以下方法(我在代码中添加了 cmets)

    $("#filters :checkbox").change(function() { // if checkbox state changes (in case a label is clicked)
        $(".widget-lijst article").hide(); // hide all the items first
    
        var selector = ''; // create a selector
      
        $("#filters :checkbox:checked").each(function() {
          selector += '.' + $(this).val();  // append selected values to selector (no spaces so classes are combined if multiple)
        });
      
        $(selector).show(); // show matching articles
    });

    【讨论】:

    • 哇,它似乎工作了!我将添加更多帖子和过滤选项,看看它是否继续有效。不幸的是,我可以在几个小时后这样做,但我肯定会回来接受你的回答。
    【解决方案2】:
    $(document).ready(function(){
        $('input:checkbox').click(function(){
            var value = $(this).val();
            $('.'+value).toggle();
        });
    });
    
    .category {
        width: 100px;
        height: 100px;
        background: #ccc;
        border: 1px solid;
        margin: 10px;
        padding: 10px;
        float: left;
        display: none;
    }
    
    <div>
        <input type="checkbox" name="article" value="category01"> Category 01
        <input type="checkbox" name="article" value="category02"> Category 02
        <input type="checkbox" name="article" value="category03"> Category 03
        <input type="checkbox" name="article" value="category04"> Category 04
    </div>
    <div>
        <div class="category category01">
            Article category01
        </div>
        <div class="category category01">
            Article category01
        </div>
        <div class="category category01">
            Article category01
        </div>
        <div class="category category01">
            Article category01
        </div>
        <div class="category category02">
            Article category02
        </div>
        <div class="category category02">
            Article category02
        </div>
        <div class="category category02">
            Article category02
        </div>
        <div class="category category03">
            Article category03
        </div>
        <div class="category category03">
            Article category03
        </div>
        <div class="category category04">
            Article category04
        </div>
    </div>
    

    这可能会解决您的问题。请检查这个。

    https://jsfiddle.net/ganesh16889/r359ysdt/

    【讨论】:

    • 嗨,不幸的是,使用我的 html 代码,这不会显示具有相同类名的文章。当一个被选中时它只会显示一个,而它应该至少显示两个。
    • 在您的脚本中,您有一个代码可以先隐藏所有文章。我的意思是这一篇 $(".widget-lijst article").hide(); 。请避免这种情况。
    • 在我的例子中。它显示了选中复选框的所有文章。
    • 我复制了你的代码并替换了它,但皮特设法提供了我想要的东西。还是谢谢你:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多