通过 DOM 通过 checkboxex 过滤列表 Div

发布于 2025-01-03 19:46:09 字数 821 浏览 0 评论 0原文

基于我之前在这里提出的问题: 通过复选框过滤列表 DIV 我遇到的情况是,复选框和列表都是从 javascript 生成的,所以如果我查看源代码,我看不到它们。 我设法发现我可以通过以下方式访问复选框:

$(".bedroomType").attr("type", "input").change(function(){}

但我无法创建要过滤的列表... 在这种情况下应该如何更改这段代码?

 $(".bedroomType").attr("type", "input").change(function(){
   $(".bedroomType").attr("type", "input").each(function(){

        var checked = $(this).attr('checked') || false;
        var numberofrooms = $(this).data('bedrooms');
        alert(numberofrooms);
        $('.listing').each(function(){
            if ($(this).data('bedrooms')==numberofrooms){
                checked ? $(this).show() : $(this).hide();
            }
        });
    });

谢谢

based my previous question asked here:
Jquery Filter List DIVs via checkboxes
i got in the situation when both checkboxes and the list is generated from javascript so if i look in view source i could't see them .
I manage to find out that i could access the checkboxes via:

$(".bedroomType").attr("type", "input").change(function(){}

but i can't make the list to filter...
How should be changed in this case this code?

 $(".bedroomType").attr("type", "input").change(function(){
   $(".bedroomType").attr("type", "input").each(function(){

        var checked = $(this).attr('checked') || false;
        var numberofrooms = $(this).data('bedrooms');
        alert(numberofrooms);
        $('.listing').each(function(){
            if ($(this).data('bedrooms')==numberofrooms){
                checked ? $(this).show() : $(this).hide();
            }
        });
    });

thank you

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

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

发布评论

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

评论(1

心头的小情儿 2025-01-10 19:46:09

您的示例代码将不起作用,因为您无法为列表中的 show()hide() 项目授予过滤权限。一般来说,过滤器应该是 show()hide() (或类似的),在这种情况下它应该是 hide()

您会发现将代码组织到调用“卧室”过滤器和(最终)其他过滤器的“主”过滤器中会更方便。主人应该确保所有列表项最初都显示,以便过滤器可以有选择地隐藏它们。

人们很容易认为您可以以相反的方式做事 - 即最初隐藏()所有项目,然后允许过滤器有选择地show()。但这是行不通的,因为任何单独的过滤器都不应该拥有 show() 的权限 - 其他过滤器可能有不同的意见!使用这种类型的过滤器集,您希望项目仅在满足所有条件(而不是任何条件)时才显示。

这是一些代码:

function filterListingByBedrooms($listing){
    var selectedBeds = [];
    $(".bedroomType").each(function(){
        if(this.checked) {
            selectedBeds.push($(this).data('bedrooms'));//build array of checked bedroom numbers
        }
    });
    $listing.each(function(){
        var $this = $(this);
        if(!$.inArray($this.data('bedrooms'), selectedBeds)) {
            $this.hide();
        }
    });
}

function filterListingByType($listing){
    ...;
}

function filterListingByLocation($listing){
    ...;
}

function filterListing(){
    var $listing = $('.listing').show();//Initially show all, then allow filters to selectively hide.
    filterListingByBedrooms($listing);
    filterListingByType($listing);//apply other filter
    filterListingByLocation($listing);//apply other filter
};

$(".bedroomType").change(filterListing);
$(".propertyType").change(filterListing);
$(".location").change(filterListing);

Your sample code won't work because you cannot give the filter authority to both show() AND hide() items in the listing. In general, a filter should either show() OR hide() (or similar), and in this case it should hide().

You will find it more convenient to organise the code into a 'master' filter that calls the 'bedroom' filter and (eventually) other filters. The master should ensure that all listing items are initially shown, so that the filters can selectively hide them.

It is tempting to think that you could alternatively do things the other way round - namely to hide() all items initially, then allow the filters to selectively show(). But this would not work because no individual filter should have the authority to show() - other filters may have a different opinion! With a filter-set of this type, you want items to show only if they meet ALL criteria, not ANY one.

Here's some code:

function filterListingByBedrooms($listing){
    var selectedBeds = [];
    $(".bedroomType").each(function(){
        if(this.checked) {
            selectedBeds.push($(this).data('bedrooms'));//build array of checked bedroom numbers
        }
    });
    $listing.each(function(){
        var $this = $(this);
        if(!$.inArray($this.data('bedrooms'), selectedBeds)) {
            $this.hide();
        }
    });
}

function filterListingByType($listing){
    ...;
}

function filterListingByLocation($listing){
    ...;
}

function filterListing(){
    var $listing = $('.listing').show();//Initially show all, then allow filters to selectively hide.
    filterListingByBedrooms($listing);
    filterListingByType($listing);//apply other filter
    filterListingByLocation($listing);//apply other filter
};

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