jQuery下拉筛选仅返回一个结果

huangapple go评论62阅读模式
英文:

jQuery Dropdown Filter Returns only one result

问题

我有一个显示活动信息的表格。其中一个列是“State”,显示活动所在的州。我希望我的州的下拉列表能筛选出表格中匹配的州。我目前的代码可以做到这一点,但它只返回最后一个匹配的州。我希望我的 jQuery 过滤器能返回所有匹配的州。举例来说,如果我的表格中有两个在得克萨斯州举行的活动 - 如果我在下拉列表中选择得克萨斯州,只会返回其中一个活动。我该如何才能显示两个得克萨斯州的活动?我相信我错过了一些简单的东西。

我的代码如下:
我有一个典型的州下拉列表的 HTML,包含所有州,但这里是一个简单的示例,没有列出每个州:

$("#filter").change(function() {
  var filterValue = $(this).val();
  var row = $('.row-conventions');
  console.log(filterValue);

  row.each(function(i, el) {
    if ($(el).attr('data-type') == filterValue) {
      row.hide();
      $(el).show();
    }
  });

  if ("all" == filterValue) {
    row.show();
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label>Select Your State:</label>
<select class="state" id="filter">
  <option value="all" selected="selected">Select a State</option>
  <option value="Alabama">Alabama</option>
  <option value="Alaska">Alaska</option>
</select>

我尝试将 .each 更新为 .map,但没有奏效。

英文:

I have a table that displays event information. One of the columns is "State" showing what State the event is being held in. I want my dropdown list of states to filter only the matching states in the table. The current code I have does this but it only returns the last matching state. I want my jQuery filter to return all the matching states. For example, if there are two events in my table happening in Texas - if I select Texas in the dropdown only one of the events is returned. How do I get it to show me both Texas events? I'm sure I'm missing something simple.

My code
I have a typical State Dropdown html with all states but here's a quick example without every state:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;#filter&quot;).change(function() {
  // console.clear();
  var filterValue = $(this).val();
  var row = $(&#39;.row-conventions&#39;);
  console.log(filterValue);

  row.each(function(i, el) {
    if ($(el).attr(&#39;data-type&#39;) == filterValue) {
      row.hide();
      $(el).show();
    }
  });

  if (&quot;all&quot; == filterValue) {
    row.show();
  }

});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;label&gt;Select Your State:&lt;/label&gt;
&lt;select class=&quot;state&quot; id=&quot;filter&quot;&gt;
  &lt;option value=&quot;all&quot; selected=&quot;selected&quot;&gt;Select a State&lt;/option&gt;
  &lt;option value=&quot;Alabama&quot;&gt;Alabama&lt;/option&gt;
  &lt;option value=&quot;Alaska&quot;&gt;Alaska&lt;/option&gt;
&lt;/select&gt;

<!-- end snippet -->

I attempted to update .each to .map but that didn't work.

答案1

得分: 0

$row("#filter").change(function() {
var filterValue = $this.val();
var row = $('.row-conventions');
console.log(filterValue);

row.hide();
row.each(function(i, el) {
    if ($(el).attr(&#39;data-type&#39;) == filterValue) {
        $(el).show();
    }
});

if (&quot;all&quot; == filterValue) {
    row.show();
}

});

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;#filter&quot;).change(function() {
            // console.clear();
            var filterValue = $(this).val();
            var row = $(&#39;.row-conventions&#39;);
            console.log(filterValue);
           
            row.hide();
            row.each(function(i, el) {
              if ($(el).attr(&#39;data-type&#39;) == filterValue) {
                $(el).show();
              }
            });

            if (&quot;all&quot; == filterValue) {
                row.show();
              }

            });

<!-- end snippet -->

I just needed to move row.hide() outside of the function.

huangapple
  • 本文由 发表于 2023年2月7日 02:55:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定