通过 DOM 通过 checkboxex 过滤列表 Div
基于我之前在这里提出的问题: 通过复选框过滤列表 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的示例代码将不起作用,因为您无法为列表中的
show()
和hide()
项目授予过滤权限。一般来说,过滤器应该是show()
或hide()
(或类似的),在这种情况下它应该是hide()
。您会发现将代码组织到调用“卧室”过滤器和(最终)其他过滤器的“主”过滤器中会更方便。主人应该确保所有列表项最初都显示,以便过滤器可以有选择地隐藏它们。
人们很容易认为您可以以相反的方式做事 - 即最初
隐藏()
所有项目,然后允许过滤器有选择地show()
。但这是行不通的,因为任何单独的过滤器都不应该拥有show()
的权限 - 其他过滤器可能有不同的意见!使用这种类型的过滤器集,您希望项目仅在满足所有条件(而不是任何条件)时才显示。这是一些代码:
Your sample code won't work because you cannot give the filter authority to both
show()
ANDhide()
items in the listing. In general, a filter should eithershow()
ORhide()
(or similar), and in this case it shouldhide()
.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 selectivelyshow()
. But this would not work because no individual filter should have the authority toshow()
- 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: