使用<select>下拉列表值过滤列表结果

时间:2017-07-05 13:12:16

标签: javascript jquery html css

我正在尝试过滤项目索引&#39;通过使用jQuery和CSS的标签。 我目前正在&lt; select&gt;中选择所选选项的.val()下拉列表并检查它是否等于.tag的内部.html()。如果它们匹配,它将使项目输入为黑色,否则它将使其变为浅灰色。 我的当前代码在项目有一个标记时有效,但一旦有多个标记就会中断。我的猜测是因为虽然有一个.tag等于所选的下拉值,但另一个.tag不是,因此使用else条件? 我错过了什么?有没有更好/更有效的方法来做到这一点? HTML &lt;主&GT;   &lt; div class =&#34; index__container&#34;&gt;      &lt; section class =&#34; index&#34;&gt;         &lt; ul class =&#34; index__header&#34;&gt;             &LT;李&GT;项目&LT; /锂&GT;             &lt; li&gt;过滤器:&lt; select id =&#34; index__filter&#34;&gt;                 &lt;选择选项=&#34;选择&#34;值=&#39; ALL&#39;&GT;所有&LT; /选项&GT;                 &lt; option value =&#34; Pedagogical Explorations&#34;&gt; Pedagogical Explorations&lt; / option&gt;                 &lt; option value =&#34; Research Production&#34;&gt; Research Production&lt; / option&gt;                 &lt; option value =&#34; Spatial Practice&#34;&gt; Spatial Practice&lt; / option&gt;                 &lt; option value =&#34; Exhibition&#34;&gt; Exhibition&lt; / option&gt;             &LT; /选择&GT;&LT; /锂&GT;             &LT;李&GT;年份&LT; /锂&GT;         &LT; / UL&GT;         &lt; a href =&#34;&#34;&gt;             &lt; ul class =&#34; index__entry&#34;&gt;                 &lt; li&gt;在西班牙重新启动Transhumance的多尺度策略&lt; / li&gt;                 &LT;李&GT;                     &lt; span class =&#34; tag&#34;&gt; Spatial Practice&lt; / span&gt;                 &LT; /锂&GT;                 &LT;李&GT; 2017&LT; /锂&GT;             &LT; / UL&GT;         &LT; / A&GT;         &lt; a href =&#34;&#34;&gt;             &lt; ul class =&#34; index__entry&#34;&gt;                 &lt; li&gt;投机后的架构&lt; / li&gt;                 &LT;李&GT;                     &lt; span class =&#34; tag&#34;&gt; Research Production&lt; / span&gt;                 &LT; /锂&GT;                 &LT;李&GT; 2017&LT; /锂&GT;             &LT; / UL&GT;         &LT; / A&GT;         &lt; a href =&#34;&#34;&gt;             &lt; ul class =&#34; index__entry&#34;&gt;                 &lt; li&gt; Polyester Merino Chair&lt; / li&gt;                 &LT;李&GT;                     &lt; span class =&#34; tag&#34;&gt; Spatial Practice&lt; / span&gt;                 &LT; /锂&GT;                 &LT;李&GT; 2017&LT; /锂&GT;             &LT; / UL&GT;         &LT; / A&GT;         &lt; a href =&#34;&#34;&gt;             &lt; ul class =&#34; index__entry&#34;&gt;                 &lt; li&gt; 1500 caracteres&lt; / li&gt;                 &LT;李&GT;                     &lt; span class =&#34; tag&#34;&gt; Spatial Practice&lt; / span&gt;                     &lt; span class =&#34; tag&#34;&gt; Research Production&lt; / span&gt;                 &LT; /锂&GT;                 &LT;李&GT; 2017&LT; /锂&GT;             &LT; / UL&GT;         &LT; / A&GT;         &lt; a href =&#34;&#34;&gt;             &lt; ul class =&#34; index__entry&#34;&gt;                 &lt; li&gt; Europan 13:Die Arbeitersiedlung&lt; / li&gt;                 &LT;李&GT;                     &lt; span class =&#34; tag&#34;&gt; Spatial Practice&lt; / span&gt;                 &LT; /锂&GT;                 &LT;李&GT; 2015&LT; /锂&GT;             &LT; / UL&GT;         &LT; / A&GT;         &lt; a href =&#34;&#34;&gt;             &lt; ul class =&#34; index__entry&#34;&gt;                 &lt; li&gt;非对称元映射&lt; / li&gt;                 &LT;李&GT;                     &lt; span class =&#34; tag&#34;&gt;教学探索&lt; / span&gt;                 &LT; /锂&GT;                 &LT;李&GT; 2017&LT; /锂&GT;              &LT; / UL&GT;         &LT; / A&GT;         &lt; a href =&#34;&#34;&gt;             &lt; ul class =&#34; index__entry&#34;&gt;                 &lt; li&gt; Chicago Architecture Biennial&lt; / li&gt;                 &LT;李&GT;                     &lt; span class =&#34; tag&#34;&gt; Spatial Practice&lt; / span&gt;                     &lt; span class =&#34; tag&#34;&gt; Research Production&lt; / span&gt;                     &lt; span class =&#34; tag&#34;&gt;展览&lt; / span&gt;&lt; / li&gt;                 &LT;李&GT; 2015&LT; /锂&GT;             &LT; / UL&GT;         &LT; / A&GT;     &LT; /节&gt; &LT; / DIV&GT; JS   //筛选项目索引   var $ indexSelect = $(&#39; #index__filter&#39;);   var $ indexEntry = $(&#39; .index__entry&#39;);   var $ tag = $(&#39; .tag&#39;);   $ indexSelect.change(函数(){     var selectedValue = $(this).val();     if(selectedValue ==&#39; ALL&#39;){        $ indexEntry.css(&#39; color&#39;,&#39; black&#39;);      返回;     }     $ tag.each(功能(I,选项){      if($(this).html()== selectedValue){         $(this).closest(&#39; ul&#39;)。css(&#39; color&#39;,&#39; black&#39;);         的console.log($(本)的.text());       } else {        $(this).closest(&#39; ul&#39;)。css(&#39; color&#39;,&#39; lightgray&#39;);       }     });   });

3 个答案:

答案 0 :(得分:0)

试试这个

var $indexSelect = $('#index__filter');
var $indexEntry = $('.index__entry');


$indexSelect.change(function() {
    var selectedValue = $(this).val();
    if (selectedValue == 'ALL') {
        $indexEntry.css('color', 'black');
        return;
    }

    $indexEntry.each(function(i, option) {
        debugger;
        var $tag = $(this).find('.tag');
        var flag = true;
        $tag.each(function(i, option) {
            if (flag == true) {
                if ($(this).html() == selectedValue) {
                    $(this).closest('ul').css('color', 'black');
                    console.log($(this).text());
                    flag = false;
                } else {
                    $(this).closest('ul').css('color', 'lightgray');
                }
            }
        });
    });
});

答案 1 :(得分:0)

这是一个简单的工作代码。你正在做的是完全正确的,除了你需要为ul分配一个类并检查它。请尝试以下代码:

&#13;
&#13;
var $indexSelect = $('#index__filter');
var $indexEntry = $('.index__entry');
var $tag = $('.tag');

$indexSelect.change(function() {
  $indexEntry.removeClass('selected');
  var selectedValue = $(this).val();
  if (selectedValue == 'ALL') {
    $indexEntry.css('color', 'black');
    return;
  }

  $tag.each(function(i, option) {
    if (!$(this).closest('ul').hasClass('selected')) {
      $(this).closest('ul').css('color', 'lightgray');
      if ($(this).html() == selectedValue) {
        $(this).closest('ul').css('color', 'black').addClass('selected');
        //console.log($(this).text());
      }
    }
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<main>
  <div class="index__container">
    <section class="index">
      <ul class="index__header">
        <li>Project</li>
        <li>Filter: <select id="index__filter">
                <option selected="selected" value='ALL'>All</option>
                <option value="Pedagogical Explorations">Pedagogical Explorations</option>
                <option value="Research Production">Research Production</option>
                <option value="Spatial Practice">Spatial Practice</option>
                <option value="Exhibition">Exhibition</option>
            </select></li>
        <li>Year</li>
      </ul>
      <a href="">
        <ul class="index__entry">
          <li>Multiscale Strategies to Reactivate Transhumance in Spain</li>
          <li>
            <span class="tag">Spatial Practice</span>
          </li>
          <li>2017</li>
        </ul>
      </a>
      <a href="">
        <ul class="index__entry">
          <li>Architecture after Speculation</li>
          <li>
            <span class="tag">Research Production</span>
          </li>
          <li>2017</li>
        </ul>
      </a>
      <a href="">
        <ul class="index__entry">
          <li>Polyester Merino Chair</li>
          <li>
            <span class="tag">Spatial Practice</span>
          </li>
          <li>2017</li>
        </ul>
      </a>
      <a href="">
        <ul class="index__entry">
          <li>1500 caracteres</li>
          <li>
            <span class="tag">Spatial Practice</span>
            <span class="tag">Research Production</span>
          </li>
          <li>2017</li>
        </ul>
      </a>
      <a href="">
        <ul class="index__entry">
          <li>Europan 13: Die Arbeitersiedlung</li>
          <li>
            <span class="tag">Spatial Practice</span>
          </li>
          <li>2015</li>
        </ul>
      </a>
      <a href="">
        <ul class="index__entry">
          <li>Asymmetric Meta-Mapping</li>
          <li>
            <span class="tag">Pedagogical Explorations</span>
          </li>
          <li>2017</li>
        </ul>
      </a>
      <a href="">
        <ul class="index__entry">
          <li>Chicago Architecture Biennial</li>
          <li>
            <span class="tag">Spatial Practice</span>
            <span class="tag">Research Production</span>
            <span class="tag">Exhibition</span></li>
          <li>2015</li>
        </ul>
      </a>
    </section>
  </div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

因为你循环遍历所有标签,并为每个标签设置了风格,它只会显示每个条目的最后一个标签的样式。

如果你为每个条目循环,而不是每个条目中的每个标签,你可以在找到与所选标签匹配的标签后突破循环。

// loop trough each entry
$indexEntry.each(function(i,entry){
  // loop trough tag iwithin the entry
  $( this ).find( '.tag' ).each(function(i, tag){
    if ($(this).html() == selectedValue) {
        $(this).closest('ul').css('color', 'black');
        // exit this loop as soon as we've determined it should be black
        return false; 
    } else {
       $(this).closest('ul').css('color', 'lightgray');
    }
  })
})

$(function(){
// Filter Project Index
  var $indexSelect = $('#index__filter');
  var $indexEntry = $('.index__entry');
  var $tag = $('.tag');

  $indexSelect.change(function(){
    var selectedValue = $(this).val();
    if(selectedValue == 'ALL'){
       $indexEntry.css('color', 'black');
     return;
    }

    $indexEntry.each(function(i,entry){
      $( this ).find( '.tag' ).each(function(i, tag){
        if ($(this).html() == selectedValue) {
            $(this).closest('ul').css('color', 'black');
            return false;
            console.log($(this).text());
          } else {
           $(this).closest('ul').css('color', 'lightgray');
          }
      })
    })

    
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<main>
  <div class="index__container">
     <section class="index">
        <ul class="index__header">
            <li>Project</li>
            <li>Filter: <select id="index__filter">
                <option selected="selected" value='ALL'>All</option>
                <option value="Pedagogical Explorations">Pedagogical Explorations</option>
                <option value="Research Production">Research Production</option>
                <option value="Spatial Practice">Spatial Practice</option>
                <option value="Exhibition">Exhibition</option>
            </select></li>
            <li>Year</li>
        </ul>
        <a href="">
            <ul class="index__entry">
                <li>Multiscale Strategies to Reactivate Transhumance in Spain</li>
                <li>
                    <span class="tag">Spatial Practice</span>
                </li>
                <li>2017</li>
            </ul>
        </a> 
        <a href="">
            <ul class="index__entry">
                <li>Architecture after Speculation</li>
                <li>
                    <span class="tag">Research Production</span>
                </li>
                <li>2017</li>
            </ul>
        </a> 
        <a href="">
            <ul class="index__entry">
                <li>Polyester Merino Chair</li>
                <li>
                    <span class="tag">Spatial Practice</span>
                </li>
                <li>2017</li>
            </ul>
        </a> 
        <a href="">
            <ul class="index__entry">
                <li>1500 caracteres</li>
                <li>
                    <span class="tag">Spatial Practice</span>
                    <span class="tag">Research Production</span>
                </li>
                <li>2017</li>
            </ul>
        </a> 
        <a href="">
            <ul class="index__entry">
                <li>Europan 13: Die Arbeitersiedlung</li>
                <li>
                    <span class="tag">Spatial Practice</span>
                </li>
                <li>2015</li>
            </ul>
        </a> 
        <a href="">
            <ul class="index__entry">
                <li>Asymmetric Meta-Mapping</li>
                <li>
                    <span class="tag">Pedagogical Explorations</span>
                </li>
                <li>2017</li>
             </ul>
        </a> 
        <a href="">
            <ul class="index__entry">
                <li>Chicago Architecture Biennial</li>
                <li>
                    <span class="tag">Spatial Practice</span>
                    <span class="tag">Research Production</span>
                    <span class="tag">Exhibition</span></li>
                <li>2015</li>
            </ul>
        </a>
    </section>
</div>