数组已设置但未定义
我正在使用 jQuery Isotope 来过滤产品照片。我必须更改示例以使用选择框,这效果很好,但现在我需要调整具有多个过滤属性的示例中的代码。
这是有效的示例代码:
$(function(){
var $container = $('#container'),
filters = {};
$container.isotope({
itemSelector : '.color-shape',
masonry: {
columnWidth: 80
}
});
// filter buttons
$('.filter a').click(function(){
var $this = $(this);
// don't proceed if already selected
if ( $this.hasClass('selected') ) {
return;
}
var $optionSet = $this.parents('.option-set');
// change selected class
$optionSet.find('.selected').removeClass('selected');
$this.addClass('selected');
// store filter value in object
// i.e. filters.color = 'red'
var group = $optionSet.attr('data-filter-group');
filters[ group ] = $this.attr('data-filter-value');
// convert object into array
var isoFilters = [];
for ( var prop in filters ) {
isoFilters.push( filters[ prop ] )
}
var selector = isoFilters.join('');
$container.isotope({ filter: selector });
return false;
});
});
这是我的代码,无效。
$(function() {
var $container = $('#productsContainer'),
filters = {};
$container.isotope({
itemSelector: '.item'
});
$('.filter select').change(function() {
var $this = $(this),
$optionSet = $this.parents('.option-set'),
$container = $('#productsContainer');
var group = $optionSet.attr('data-filter-group');
filters[group] = $this.attr('data-filter-value');
// convert object into array
var isoFilters = [];
for (var prop in filters) {
isoFilters.push(filters[prop])
}
var selector = isoFilters.join('');
$container.isotope({
filter: selector
});
return false;
});
});
我已经追踪到正在添加的 isoFilters 数组项,但它们是空的。我不知道问题出在哪里。
如果我替换这一行,代码就可以工作:
var selector = isoFilters.join('');
用这个:
var selector = $("option:selected",this).attr("data-filter-value");
但这不允许按两个组进行过滤,只能按一个组进行过滤。
我对此有点困惑,希望得到一些帮助。
谢谢。
I'm using jQuery Isotope to filter product photos. I've had to change the example to use select boxes, which works great, but now I need to adapt the code from the example that has multiple filtering attributes.
This is the example code that works:
$(function(){
var $container = $('#container'),
filters = {};
$container.isotope({
itemSelector : '.color-shape',
masonry: {
columnWidth: 80
}
});
// filter buttons
$('.filter a').click(function(){
var $this = $(this);
// don't proceed if already selected
if ( $this.hasClass('selected') ) {
return;
}
var $optionSet = $this.parents('.option-set');
// change selected class
$optionSet.find('.selected').removeClass('selected');
$this.addClass('selected');
// store filter value in object
// i.e. filters.color = 'red'
var group = $optionSet.attr('data-filter-group');
filters[ group ] = $this.attr('data-filter-value');
// convert object into array
var isoFilters = [];
for ( var prop in filters ) {
isoFilters.push( filters[ prop ] )
}
var selector = isoFilters.join('');
$container.isotope({ filter: selector });
return false;
});
});
And here is my code that doesn't work.
$(function() {
var $container = $('#productsContainer'),
filters = {};
$container.isotope({
itemSelector: '.item'
});
$('.filter select').change(function() {
var $this = $(this),
$optionSet = $this.parents('.option-set'),
$container = $('#productsContainer');
var group = $optionSet.attr('data-filter-group');
filters[group] = $this.attr('data-filter-value');
// convert object into array
var isoFilters = [];
for (var prop in filters) {
isoFilters.push(filters[prop])
}
var selector = isoFilters.join('');
$container.isotope({
filter: selector
});
return false;
});
});
I've tracked it down to the isoFilters array items being added, but they are empty. I can't figure out what the problem is.
The code works if I replace this line:
var selector = isoFilters.join('');
With this:
var selector = $("option:selected",this).attr("data-filter-value");
But that doesn't allow to to filter by both groups, only one.
I'm kind of over my head on this, hoping for some help.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我明白发生了什么事。该行没有从选择框中选择正确的属性。
我不得不把它改成这样:
I figured out what what going on. This line wasn't selecting the proper attribute from the select box.
I had to change it to this: