最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - MixItUp with two selectdropdown fields - Stack Overflow

programmeradmin0浏览0评论

I'm not advanced enough in JS to get this working fully.

I have two select fields with options:

    <select name="" id="filter-position">
      <option value="all">Filter by position</option>
      <option value=".instructor">Instructor</option>
      <option value=".leader">Leader</option>
      <option value=".blah">Blah</option>
      <option value=".whatever">Whatever</option>
    </select>

    <select name="" id="filter-location">
      <option value="all">Filter by position</option>
      <option value=".portland">Portland</option>
      <option value=".missoula">Missoula</option>
      <option value=".chicago">Chicago</option>
      <option value=".newyork">New York</option>
    </select>

The container where the filtered items live looks a little like this:

    <ul id="filter-container">
        <li class="mix">...</li>
        <li class="mix">...</li>
        <li class="mix">...</li>
        <li class="mix">...</li>
        <li class="mix">...</li>
        <li class="mix">...</li>
    </ul>

I can get each one to filter properly but not together (i.e., AND). The JS code I'm using goes like this:

  $(function(){
    var $positionSelect = $('#filter-position'),
        $locationSelect = $('#filter-location'),
        $container = $('#filter-container');

    $container.mixItUp({});

    $positionSelect.on('change', function(){
      $container.mixItUp('filter', this.value);
    });

    $locationSelect.on('change', function(){
      $container.mixItUp('filter', this.value);
    });
  });

If I filter by one select, things filter properly. If I then filter by the other, it overrides the first filter. What I want is for them to both be used.

I know there's a way to get this to work, but I'm not sure how to get that concatenated string to do the "and" logic as documented in the Advanced Filtering section of the docs using <select>. Help?

I'm not advanced enough in JS to get this working fully.

I have two select fields with options:

    <select name="" id="filter-position">
      <option value="all">Filter by position</option>
      <option value=".instructor">Instructor</option>
      <option value=".leader">Leader</option>
      <option value=".blah">Blah</option>
      <option value=".whatever">Whatever</option>
    </select>

    <select name="" id="filter-location">
      <option value="all">Filter by position</option>
      <option value=".portland">Portland</option>
      <option value=".missoula">Missoula</option>
      <option value=".chicago">Chicago</option>
      <option value=".newyork">New York</option>
    </select>

The container where the filtered items live looks a little like this:

    <ul id="filter-container">
        <li class="mix">...</li>
        <li class="mix">...</li>
        <li class="mix">...</li>
        <li class="mix">...</li>
        <li class="mix">...</li>
        <li class="mix">...</li>
    </ul>

I can get each one to filter properly but not together (i.e., AND). The JS code I'm using goes like this:

  $(function(){
    var $positionSelect = $('#filter-position'),
        $locationSelect = $('#filter-location'),
        $container = $('#filter-container');

    $container.mixItUp({});

    $positionSelect.on('change', function(){
      $container.mixItUp('filter', this.value);
    });

    $locationSelect.on('change', function(){
      $container.mixItUp('filter', this.value);
    });
  });

If I filter by one select, things filter properly. If I then filter by the other, it overrides the first filter. What I want is for them to both be used.

I know there's a way to get this to work, but I'm not sure how to get that concatenated string to do the "and" logic as documented in the Advanced Filtering section of the docs using <select>. Help?

Share Improve this question asked Oct 22, 2015 at 22:37 A HerreraA Herrera 6121 gold badge5 silver badges17 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 5

You're really close! Because of how the mixItUp API works, filters aren't additive (e.g. when you call mixItUp.filter, it doesn't add a new filter to what's already active). So, you just need to construct a new filter string from the values of both dropdowns whenever one of them changes. You can acplish that like so:

$(function(){
    var $positionSelect = $('#filter-position'),
        $locationSelect = $('#filter-location'),
        $container = $('#filter-container');

    $container.mixItUp({});

    $('#filter-position, #filter-location').on('change', function() {

      var filterString = $positionSelect.val() + $locationSelect.val();

      $container.mixItUp('filter', filterString);
    });

  });

I opted to use the jQuery .val() over the native .value in this case since you're already using jQuery, and val() will probably be more reliable in this case. Let me know if you run into trouble.

You may also want to to change this:

<option value="all">Filter by position</option>

To this:

<option value="">Filter by position</option>

for both of your dropdown menus, otherwise you might get some weird behavior.

Use ma like.. $('#filter-location, #filter-container') .on('change', function(){ ... ;)

Something like this...

$(function(){
    $container = $('#filter-container');

    $container.mixItUp({});

    $container.on('change','#filter-position, #filter-location', function(){
        $container.mixItUp('filter', this.value);
    });
});
发布评论

评论列表(0)

  1. 暂无评论