数组已设置但未定义

发布于 2024-12-08 20:48:35 字数 2281 浏览 1 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(1

花桑 2024-12-15 20:48:35

我明白发生了什么事。该行没有从选择框中选择正确的属性。

filters[group] = $this.attr('data-filter-value');

我不得不把它改成这样:

filters[ group ] = $("option:selected",this).attr('data-filter-value');

I figured out what what going on. This line wasn't selecting the proper attribute from the select box.

filters[group] = $this.attr('data-filter-value');

I had to change it to this:

filters[ group ] = $("option:selected",this).attr('data-filter-value');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文