jquery多个下拉菜单 [英] jquery multiple dropdown filter menu

查看:122
本文介绍了jquery多个下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个隐藏和显示的图片基于多个下拉菜单选择。我试图让2和可能更多的下拉菜单合作来完善搜索。



如果我在第一个下拉列表中选择一个项目,我想应用第二个过滤器等等,并附加任何额外的下拉列表。



我需要jQuery的一些帮助。我当前的问题是第二个过滤器从不踢,它被覆盖并重置过滤器。如果可能,我想添加一个第三个过滤器来缩小搜索范围,甚至找到我正在寻找的内容。这里是一些示例代码。

  $('select')。change(function(){
var current = $(this).attr('value');

if(current =='all'){
$('#FilterContainer')。children('div.all' ).show();
}

else {

$('#FilterContainer')。children('div:not(。'+ current + )')hide();
$('#FilterContainer')。children('div。'+ current).show();
}

return false;
})

HTML

 < p>过滤器:< / p> 
< select class =filterby>
< option value =all>< h5>显示全部< / h5>< / option>
< option value =1>< h5> One< / h5>< / option>
< option value =2>< h5> Two< / h5>< / option>
< option value =3>< h5> Three< / h5>< / option>
< / select>



< p>位置:< / p>
< select class =filterby>
< option value =all>< h5>所有位置< / h5>< / option>
< option value =nj>< h5> NJ< / h5>< / option>
< option value =ny>< h5> NY< / h5>< / option>
< option value =pa>< h5> PA< / h5>< / option>
< / select>


< div id =FilterContainer>

< div class =all 1 nj> Test One NJ< / div>
< div class =all 1 ny> Test One NY< / div>
< div class =all 1 pa> Test One PA< / div>
< div class =all 2 nj> Test Two NJ< / div>
< div class =all 2 ny> Test Two NY< / div>
< div class =all 2 pa>测试两个PA< / div>
< div class =all 3 nj> Test Three NJ< / div>
< div class =all 3 ny> Test Three NY< / div>
< div class =all 3 pa>测试三个PA< / div>
< div class =all 1 nj> Test One NJ< / div>
< div class =all 1 pa> Test One PA< / div>
< div class =all 1 pa> Test One PA< / div>
< div class =all 2 nj> Test Two NJ< / div>
< div class =all 2 ny> Test Two NY< / div>
< div class =all 2 ny> Test Two NY< / div>


解决方案

尝试这个 -

  $(select.filterby)。change(function(){
var filters = $ .map($(select.filterby) .toArray(),function(e){
return $(e).val();
})。join(。);
$(div#FilterContainer .find(div)。hide();
$(div#FilterContainer)。find(div。+ filters).show();
});

LIVE DEMO


I have pictures hiding and showing based on a multiple dropdown menu selection. I am trying to have the 2 and possible more dropdown menus working together to refine a search.

If I select an item in the first dropdown, I would like to apply the second filter and so on with any additional dropdown.

I need some help with the jquery. My current problem is the 2nd filter never kicks in. It gets overwritten and resets the filter. If possible, I would like to add a 3rd filter to narrow the search even more to find exactly what I am looking for. Here is some sample code..

$('select').change(function(){
var current = $(this).attr('value');

if(current == 'all'){
    $('#FilterContainer').children('div.all').show();
} 

else {

    $('#FilterContainer').children('div:not(.' + current + ')').hide();
$('#FilterContainer').children('div.' + current).show();
 }

  return false;
})

HTML

  <p>Filter: </p>
        <select class="filterby">
        <option value="all"><h5>Show All</h5></option>
        <option value="1"><h5>One</h5></option>
        <option value="2"><h5>Two</h5></option>
        <option value="3"><h5>Three</h5></option>
      </select>



      <p>Location: </p>
        <select class="filterby">
        <option value="all"><h5>All Locations</h5></option>
        <option value="nj"><h5>NJ</h5></option>
        <option value="ny"><h5>NY</h5></option>
        <option value="pa"><h5>PA</h5></option>
      </select>


  <div id="FilterContainer">

  <div class="all 1 nj">Test One NJ</div>
  <div class="all 1 ny">Test One NY</div>
  <div class="all 1 pa">Test One PA</div>
  <div class="all 2 nj">Test Two NJ</div>
  <div class="all 2 ny">Test Two NY</div>
  <div class="all 2 pa">Test Two PA</div>
  <div class="all 3 nj">Test Three NJ</div>
  <div class="all 3 ny">Test Three NY</div>
  <div class="all 3 pa">Test Three PA</div>
  <div class="all 1 nj">Test One NJ</div>
  <div class="all 1 pa">Test One PA</div>
  <div class="all 1 pa">Test One PA</div>
  <div class="all 2 nj">Test Two NJ</div>
  <div class="all 2 ny">Test Two NY</div>
  <div class="all 2 ny">Test Two NY</div>

解决方案

Try this -

$("select.filterby").change(function(){
    var filters = $.map($("select.filterby").toArray(), function(e){
        return $(e).val();
    }).join(".");
    $("div#FilterContainer").find("div").hide();
    $("div#FilterContainer").find("div." + filters).show();
});

with LIVE DEMO

这篇关于jquery多个下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆