jQuery 按帖子过滤

发布于 2024-11-27 11:13:46 字数 2718 浏览 5 评论 0原文

我有一个显示自定义帖子的存档页面,每个帖子都有一个 div 在 div 中显示帖子的分类术语(自定义类别“jobtype”),如下所示:

<div class="hidden tags category1 category2 cat3 etc..."></div>

然后我有一个带有复选框的表单具有与帖子可能具有的某些可能类别相对应的值:

<input class="filterbox" type="checkbox" name="interest" value="academia" />Academia<br />
<input class="filterbox" type="checkbox" name="interest" value="adminassistant" />Admin/Assistant<br />
<input class="filterbox" type="checkbox" name="interest" value="communicationsmarketing" />Communications/Marketing<br />
<input class="filterbox" type="checkbox" name="interest" value="development" />Development<br />

我想使用 jQuery 来过滤(读取:$('.postclass').hide();)基于以下内容的当前帖子选中的复选框。目前我正在使用以下 jQuery 代码:

var $checkboxes;

function storecat() {         
    var tags = $checkboxes.map(function() {
        if(this.checked) return this.value;
    }).get().join(' ');
    $('#selected-interests').html(tags); 
}

$(function() {
    $checkboxes = $('.filterbox:checkbox').change(storecat);
});

$('#filtersubmit').click(){

}

这是一个帖子条目在类别页面上的样子的乏味示例:

<div class="post_entry">
    <div class="post-thumb"></div>
    <h4 class="post-title"><a href="#" title="Title">Title</a></h4>
<div class="post-meta">
    <p class="post_meta_organization">Job Organization</p>
        <p class="post_meta_location">City, State</p>
        <p class="post_meta_url">Website: <a href="#">Click Here</a></p>
    </div>
    <div class="hidden tags early-career internship other web-developmentit"></div>
    <p class="post_excerpt">Post Content Here</p>
</div>

我想我需要执行 $('.post_entry').each(); 检查 var tags,但我完全不确定从哪里开始。我认为我需要的是创建一个标签数组(现在他们正在创建一个列表?),并在

我希望它隐藏没有任何相关标签的帖子,但只显示选择了所有标签的帖子。如果有人可以帮助我的 .each() 函数,我将不胜感激。

如果您想查看更多布局,请访问 http://www.cirkut.net/ wp/libertyguide/jobs

如果需要更多信息,请询问。

子问题

如果没有选择任何输入,如何使所有帖子显示?我没有将其作为新问题发布,因为我发现这与该问题高度相关。

如果我有一个提交按钮

并且我将 jQuery 更改为

$('#filter_submit').click( function() {
    $('.tags').parent().hide();
    $('input:checked').each( function(i) {
    $('div.' + $(this).val()).parent().show();
});

当我单击提交按钮时,所有帖子都会隐藏。有什么帮助吗?

I have an archive page that shows custom posts, each of which has a div that shows the post's taxonomy terms (custom categories 'jobtype') in a div like so:

<div class="hidden tags category1 category2 cat3 etc..."></div>

Then I have a form with checkboxes each with a value that corresponds to some possible categories that the posts can have:

<input class="filterbox" type="checkbox" name="interest" value="academia" />Academia<br />
<input class="filterbox" type="checkbox" name="interest" value="adminassistant" />Admin/Assistant<br />
<input class="filterbox" type="checkbox" name="interest" value="communicationsmarketing" />Communications/Marketing<br />
<input class="filterbox" type="checkbox" name="interest" value="development" />Development<br />

I want to use jQuery to filter (read: $('.postclass').hide();) the current posts based on the checkboxes selected. Currently I'm using the following jQuery code:

var $checkboxes;

function storecat() {         
    var tags = $checkboxes.map(function() {
        if(this.checked) return this.value;
    }).get().join(' ');
    $('#selected-interests').html(tags); 
}

$(function() {
    $checkboxes = $('.filterbox:checkbox').change(storecat);
});

$('#filtersubmit').click(){

}

And this is a dulled example of what a post entry looks like on the category page:

<div class="post_entry">
    <div class="post-thumb"></div>
    <h4 class="post-title"><a href="#" title="Title">Title</a></h4>
<div class="post-meta">
    <p class="post_meta_organization">Job Organization</p>
        <p class="post_meta_location">City, State</p>
        <p class="post_meta_url">Website: <a href="#">Click Here</a></p>
    </div>
    <div class="hidden tags early-career internship other web-developmentit"></div>
    <p class="post_excerpt">Post Content Here</p>
</div>

I think I need to do a $('.post_entry').each(); to check against the var tags, but I'm entirely unsure of where to begin. What I think I need is to create an array of tags (right now they're creating a list?), and create an array of classes in the <div class="hidden tags..."></div>

I want it to hide posts that don't have any relevant tags, but only show posts that have all the tags selected. If anyone can help out with my .each() function it would be highly appreciated.

If you want to check out more of the layout, go to http://www.cirkut.net/wp/libertyguide/jobs

If any more info is needed, please ask.

SUB-QUESTION:

How would make all posts show if NO inputs are selected? I didn't post this as a new question because I find this highly relevant to this question.

If I have a submit button <input id="filter_submit" value="FIND" />

and I alter the jQuery to be

$('#filter_submit').click( function() {
    $('.tags').parent().hide();
    $('input:checked').each( function(i) {
    $('div.' + $(this).val()).parent().show();
});

When I click the submit button, all the posts hide. Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

緦唸λ蓇 2024-12-04 11:13:46

这就是您要找的吗: http://jsfiddle.net/3qwty/1/

$('input').click( function() {
    $('.tags').hide();
    $('input:checked').each( function() {
        $('div.' + $(this).val()).show();
    });
});

当前设置为仅显示选定的项目,但您可以翻转它以隐藏选定的项目(即 http://jsfiddle.net/3qwty/2/)。

编辑:

看起来您可能隐藏了post-meta的容器div,在这种情况下您可以这样做:
http://jsfiddle.net/3qwty/3/,隐藏父容器

$('input').click( function() {
    $('.tags').parent().show();
    $('input:checked').each( function() {
        $('div.' + $(this).val()).parent().hide();
    });
});

SUBQUESTION编辑:

修改为在清除所有复选框时“重新显示”所有帖子:
http://jsfiddle.net/3qwty/8/

$('input').click( function() {
    $('.tags').parent().hide();
    $('input:checked').each( function() {
        $('div.' + $(this).val()).parent().show();
    });
    if (!$('input:checked').length) $('.tags').parent().show();
});

Would this be what you're looking for: http://jsfiddle.net/3qwty/1/

$('input').click( function() {
    $('.tags').hide();
    $('input:checked').each( function() {
        $('div.' + $(this).val()).show();
    });
});

It's currently set to only show the items that are selected, but you could flip it to hide the items that are selected instead (i.e., http://jsfiddle.net/3qwty/2/).

EDIT:

It looks like you might be hiding the container div of post-meta, in which case you could do this:
http://jsfiddle.net/3qwty/3/, which hides the parent container

$('input').click( function() {
    $('.tags').parent().show();
    $('input:checked').each( function() {
        $('div.' + $(this).val()).parent().hide();
    });
});

SUBQUESTION EDIT:

Modified to 're-show' all posts when all checkboxes are cleared:
http://jsfiddle.net/3qwty/8/

$('input').click( function() {
    $('.tags').parent().hide();
    $('input:checked').each( function() {
        $('div.' + $(this).val()).parent().show();
    });
    if (!$('input:checked').length) $('.tags').parent().show();
});
九八野马 2024-12-04 11:13:46

一种可能的解决方案是隐藏具有“post_entry”类的所有帖子,然后仅显示具有所有选定类别的帖子:

$(".post_entry").hide().filter(".category1.category2.categoryN").show();

jquery filter() 列出的类不带空格,给出所选项目的交集;如果您想要合并(至少具有一个类别的项目),请使用过滤器 ".category1, .category2, .categoryN"

A possible solution to hide all posts with class "post_entry", then show only those which have all selected categories:

$(".post_entry").hide().filter(".category1.category2.categoryN").show();

jquery filter() with classes listed with no spaces gives an intersection of the selected items; if you want union (the items with at least one category), use filter with ".category1, .category2, .categoryN"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文