Jquery 通过多个数字范围进行过滤

发布于 2024-11-28 07:23:27 字数 3331 浏览 1 评论 0原文

我认为我陷入了困境......我正在尝试获取项目列表并根据对象的属性创建过滤器。我将其简化为一个更简单的书籍示例,其中包含成本和年份。我当前在页面上有一个书籍列表和过滤器(复选框),可以选择这些过滤器仅显示一定成本和/或年份范围内的书籍。这是我到目前为止的代码:

<div id="filters">
<h1>FILTERS</h1>
<div class="filter filter_cost">
    <input class="target" type="checkbox" min="0" max="9" />Under $10.00<br/>
    <input class="target" type="checkbox" min="10" max="19" />$10-$19<br/>
    <input class="target" type="checkbox" min="20" max="29" />$20-$29<br/>
    <input class="target" type="checkbox" min="30" max="39" />$30-$39<br/>
    <input class="target" type="checkbox" min="40" max="1000" />$40 and Over<br/>
</div>
<div class="filter filter_year">
    <input class="target" type="checkbox" min="1700" max="1799" />18th Century<br/>
    <input class="target" type="checkbox" min="1800" max="1899" />19th Century<br/>
    <input class="target" type="checkbox" min="1900" max="1999" />20th Century<br/>
    <input class="target" type="checkbox" min="2000" max="2999" />21st Centruy<br/>
</div>
</div>
<div id="books">
<h1>BOOKS</h1>
<div class="book">
    <h1>Book 1</h1>
    <input type="hidden" name="cost" value="13" />
    <input type="hidden" name="year" value="1997" />
</div>
<div class="book">
    <h1>Book 2</h1>
    <input type="hidden" name="cost" value="22" />
     <input type="hidden" name="year" value="1872" />
</div>
</div>

和我的 jQuery(使用 1.6.2):

$(document).ready(function () {
$("input.target").change(function () {
    filterResults();
});
});

function filterResults(){
$(".book").each(function () {
    var cost = $(this).find("input[name='cost']").val();
    var year = $(this).find("input[name='year']").val();
    var cover = $(this).find("input[name='cover']").val();
    var isHidden = false;
    //console.log("Cost in Range: "+filterRange(cost, ".filter_cost"));
    //console.log("Year in Range: "+filterRange(year, ".filter_year"));

    var filterCost = filterRange(cost, ".filter_cost")?showBook($(this)):hideBook($(this));
    var filterYear = filterRange(year, ".filter_year")?showBook($(this)):hideBook($(this));
    isHidden?"":filterCost;
    isHidden?"":filterYear;

    function showBook(obj) {
        obj.show();
    }

    function hideBook(obj) {
        isHidden = true;
        obj.hide();
    }

})
}

function filterRange(amount, elem) {
var checkedInputs = $(elem).find("input:checked").length;
var totalInputs = $(elem).find("input").length;
var inRange = function(){
    $(elem).find("input:checked").each(function () {
        var min = $(this).attr('min');
        var max = $(this).attr('max');
        if(amount >= min && amount <= max){
            return true;
        } else {
            return false;
        }
    });
};
if(checkedInputs == 0 || totalInputs == checkedInputs ){
    return true;
} 
if(inRange()){
    return true;
} else {
    return false;
}
}

我的问题是,在 filterRange 函数中,我不确定如何根据每个输入创建一系列条件已检查。因此价格范围可能是 10-19 和 30-39。我的尝试(var inRange)是遍历每个检查的输入,检查成本是否在范围内,然后返回 true,否则返回 false。我想我只是从根本上偏离了轨道,不确定这种方法是否有效。任何意见将不胜感激。

Think I'm getting stuck... I'm attempting to take a list of items and create filters based on attributes to the object. I stripped it down into an easier example of books with a cost and year. I currently have a list of books on the page and filters (checkboxes) that can be selected to only show books within a range of cost and/or year. Here is the code I have so far:

<div id="filters">
<h1>FILTERS</h1>
<div class="filter filter_cost">
    <input class="target" type="checkbox" min="0" max="9" />Under $10.00<br/>
    <input class="target" type="checkbox" min="10" max="19" />$10-$19<br/>
    <input class="target" type="checkbox" min="20" max="29" />$20-$29<br/>
    <input class="target" type="checkbox" min="30" max="39" />$30-$39<br/>
    <input class="target" type="checkbox" min="40" max="1000" />$40 and Over<br/>
</div>
<div class="filter filter_year">
    <input class="target" type="checkbox" min="1700" max="1799" />18th Century<br/>
    <input class="target" type="checkbox" min="1800" max="1899" />19th Century<br/>
    <input class="target" type="checkbox" min="1900" max="1999" />20th Century<br/>
    <input class="target" type="checkbox" min="2000" max="2999" />21st Centruy<br/>
</div>
</div>
<div id="books">
<h1>BOOKS</h1>
<div class="book">
    <h1>Book 1</h1>
    <input type="hidden" name="cost" value="13" />
    <input type="hidden" name="year" value="1997" />
</div>
<div class="book">
    <h1>Book 2</h1>
    <input type="hidden" name="cost" value="22" />
     <input type="hidden" name="year" value="1872" />
</div>
</div>

And my jQuery (using 1.6.2):

$(document).ready(function () {
$("input.target").change(function () {
    filterResults();
});
});

function filterResults(){
$(".book").each(function () {
    var cost = $(this).find("input[name='cost']").val();
    var year = $(this).find("input[name='year']").val();
    var cover = $(this).find("input[name='cover']").val();
    var isHidden = false;
    //console.log("Cost in Range: "+filterRange(cost, ".filter_cost"));
    //console.log("Year in Range: "+filterRange(year, ".filter_year"));

    var filterCost = filterRange(cost, ".filter_cost")?showBook($(this)):hideBook($(this));
    var filterYear = filterRange(year, ".filter_year")?showBook($(this)):hideBook($(this));
    isHidden?"":filterCost;
    isHidden?"":filterYear;

    function showBook(obj) {
        obj.show();
    }

    function hideBook(obj) {
        isHidden = true;
        obj.hide();
    }

})
}

function filterRange(amount, elem) {
var checkedInputs = $(elem).find("input:checked").length;
var totalInputs = $(elem).find("input").length;
var inRange = function(){
    $(elem).find("input:checked").each(function () {
        var min = $(this).attr('min');
        var max = $(this).attr('max');
        if(amount >= min && amount <= max){
            return true;
        } else {
            return false;
        }
    });
};
if(checkedInputs == 0 || totalInputs == checkedInputs ){
    return true;
} 
if(inRange()){
    return true;
} else {
    return false;
}
}

My issue is that in the filterRange function I'm not sure how to create a range of conditionals based on each input that is checked. So that a price range could be 10-19 and 30-39. My attempt (var inRange) was to go through each checked input, check if the cost was with in the range, then return true, else return false. I think I'm just fundamentally getting off track and unsure if this method would work at all. Any input would be much appreciated.

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

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

发布评论

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

评论(2

聚集的泪 2024-12-05 07:23:27

在 jquery 中,dom 元素返回语句上的每个循环都会跳出循环。所以你的实现是错误的。试试这个。

function filterRange(amount, elem) {
    var checkedInputs = $(elem).find("input:checked").length;
    var totalInputs = $(elem).find("input").length;
    var returnValue = false;

    $(elem).find("input:checked").each(function () {
        var min = $(this).attr('min');
        var max = $(this).attr('max');
        if(amount >= min && amount <= max){
            returnValue = true;
            return true;
        } 
     });

   return (checkedInputs == 0 || totalInputs == checkedInputs || returnValue );
}

In the jquery each loop on dom element return statement breaks out of the loop. So your implemenation is wrong. Try this.

function filterRange(amount, elem) {
    var checkedInputs = $(elem).find("input:checked").length;
    var totalInputs = $(elem).find("input").length;
    var returnValue = false;

    $(elem).find("input:checked").each(function () {
        var min = $(this).attr('min');
        var max = $(this).attr('max');
        if(amount >= min && amount <= max){
            returnValue = true;
            return true;
        } 
     });

   return (checkedInputs == 0 || totalInputs == checkedInputs || returnValue );
}
逐鹿 2024-12-05 07:23:27

尝试:

function filterRange(amount, elem) {
    var checkedInputs = $(elem).find("input:checked").length;
    var totalInputs = $(elem).find("input").length;
    var inRange = false;
    $(elem).find("input:checked").each(function () {
        var min = $(this).attr('min');
        var max = $(this).attr('max');
        if (amount >= min && amount <= max) {
            inRange = true;
            return false;
        }
    });

    if (checkedInputs == 0 || totalInputs == checkedInputs) {
        return true;
    }
    if (inRange) {
        return true;
    } else {
        return false;
    }
}

Try:

function filterRange(amount, elem) {
    var checkedInputs = $(elem).find("input:checked").length;
    var totalInputs = $(elem).find("input").length;
    var inRange = false;
    $(elem).find("input:checked").each(function () {
        var min = $(this).attr('min');
        var max = $(this).attr('max');
        if (amount >= min && amount <= max) {
            inRange = true;
            return false;
        }
    });

    if (checkedInputs == 0 || totalInputs == checkedInputs) {
        return true;
    }
    if (inRange) {
        return true;
    } else {
        return false;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文