简体   繁体   English

在 jQuery 中选择特定的 class

[英]Selecting a specific class in jQuery

I'm trying to filter different posts kinda page.我正在尝试过滤不同的帖子有点页面。 Each post has many different categories.每个帖子都有许多不同的类别。 But the search filter is two-layered, the main filter and then below a more specific selection of filters where I used checkboxes.但是搜索过滤器是两层的,主过滤器,然后是我使用复选框的更具体的过滤器选择。 The problem is that all categories are on the same level.问题是所有类别都处于同一水平。 How can I access each selected class based on the user filter input and then output the right post?如何根据用户过滤器输入访问每个选定的 class 然后 output 正确的帖子?

Categories and their classes are listed like this:类别及其类如下所示:

    <span class="category">
        <span class="cat Event">Event</span>
        <span class="cat Developing">Developing</span>
        <span class="cat SQL">SQL</span>
      </span> 

Where "Event" is in the main filter and the other two are in the second checkbox filter

The project is done in MVC .NET and for filtering functionality, I'm using jQuery该项目在 MVC .NET 中完成,对于过滤功能,我使用的是 jQuery

This is how to get each post in my view:这是在我看来如何获取每个帖子的方法:

 <div class="novice-list">
        @foreach (var item in Model.Articles)
        {
            <div class="novica-container">
                <a href="@DataContext.Current.RouteUrlManager.GetModuleLink("article", null, "details", item.Id, item.Title)">
                    <div class="media">
                        @{
                            string slika = string.Empty;
                            if (item.Id > 166 || item.Id == 159)
                            {
                                slika = $"{WellKnownStrings.StorageBaseUrl}{WellKnownStrings.ArticleImageContainer}/{item.FeaturedImage}";
                            }
                            else
                            {
                                slika = item.FeaturedImageUrl;
                            }
                        }
                        <div class="slika" style="background-image: url('@if (item.FeaturedImageUrl != "") { @slika }');">
                        </div>
                        <div class="media-body">
                            @*<a href="#">content</a>*@
                            <div class="meta">
                                <span class="published">@item.DateFormated</span>
                                <span class="category">
                                    @foreach (var cat in @item.Category)
                                    {
                                        <span class="cat @cat.Title">@cat.Title</span>
                                    }
                                </span>
                            </div>
                            <h2>@item.Title</h2>
                            <p>@item.Summary</p>
                        </div>
                    </div>
                </a>
            </div>
        }
    </div>

this is how I get matches from first (Main) filtering:这就是我从第一个(主要)过滤中获得匹配的方式:

                isCheckedMain = true;
              
                if (isCheckedMain) {
                    var selectedClass = $(this).attr('class');


                    // reset the active class on all the buttons
                    $('#filterOptions li').removeClass('show');
                    // update the active state on our clicked button
                    $(this).parent().addClass('show');

                    if (selectedClass == 'all') {
                        // show all our items
                        $('.novica-container').slideDown(1000, "linear");
                    }
                    else {
                        // hide all elements that don't share ourClass
                        $('.novica-container').hide();
                        // show all elements that do share ourClass
                        $('.novica-container').find('span.cat.' + selectedClass).parents('.novica-container').slideDown(1000, "linear");
                    }

                }

And matches for checkbox filter:并匹配复选框过滤器:

     $(".checkbox-filter :checkbox").click(function () {
    
    
        isBoxChecked = true;
        if (isCheckedMain && isBoxChecked) {
    
var selectedBox = $(this).attr('id');
var selectedMain = selectedClass;
  
        if ($('input#' + selectedBox).is(':checked')) {
        if ($('span.cat').hasClass(selectedMain)) {
            $('.novica-container').hide();
            $('span.cat.'+selectedMain).addClass(selectedBox);               
                                    
           $('.checkbox-filter :checkbox:checked').each(function () {
                    //Get the selected checkbox value  
      var selectedBox = $(this).attr('id');
   $('.novica-container').find('span.cat.' + selectedMain  ,'.'+selectedBox).parents('.novica-container').slideDown(700);
                                       
  });
   } 
    }
       else if ($(this).is(':not(checked')) {
               $('.novica-container').find('span.cat.' + selectedBox).parents('.novica-container').slideUp(700);
    
      if (($('input[type="checkbox"]:checked').length === 0)) {
                      isBoxChecked = false;
            $('.novica-container').slideDown(1000, "linear");

This is what I have so far, I'm trying to do something with true-false( if one is true, search only with the main filter, if both are true search more accurately with other categories. And I've been stuck here for days trying to merge both sides of the code. They both work separately but not together这就是我到目前为止所拥有的,我正在尝试用真假做一些事情(如果一个是真的,只用主过滤器搜索,如果两者都是真的用其他类别更准确地搜索。我一直被困在这里几天来试图合并代码的两边。它们都单独工作,但不能一起工作

Picture of filter过滤器图片

Found a solution for this.找到了解决方案。

I added all of the categories names one level higher in my View .我在View中添加了所有类别名称更高一级。 So that class="category" now has values of all the other categories.因此 class="category" 现在具有所有其他类别的值。

Example: class="category Event Developing SQL"示例:class="category 事件开发 SQL"

And then I could just use this jQuery to filter selected posts然后我可以使用这个 jQuery 过滤选定的帖子

 $('span.category.' + selectedMain + '.' + selectedBox).parents('.novica-container').slideDown(700);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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