Javascript jquery基于数据属性显示/隐藏多个元素

Javascript jquery基于数据属性显示/隐藏多个元素,javascript,jquery,html,Javascript,Jquery,Html,您好,我正在使用jQuery过滤页面上的元素列表,该页面使用一个包含客户端ID列表的HTML select。页面上的HTML元素的数据属性为dataclient。我使用jquery筛选出不匹配的。然而,我的代码隐藏了所有元素。我的代码如下 <select class="form-control placeholder" id="filterSelect" name="clients"> <option value="14">Client 14</option&g

您好,我正在使用jQuery过滤页面上的元素列表,该页面使用一个包含客户端ID列表的HTML select。页面上的HTML元素的数据属性为
dataclient
。我使用jquery筛选出不匹配的。然而,我的代码隐藏了所有元素。我的代码如下

<select class="form-control placeholder" id="filterSelect" name="clients">
  <option value="14">Client 14</option>
  <option value="45">Client 45</option>
  <option value="48">Client 48</option>
</select>

<div class="filterable">
  <div class="filter" data-client="14">
    <h3>Hello World</h3>
    <h4>Client 14</h4>
  </div>
  <div class="filter" data-client="45">
    <h3>Hello World</h3>
    <h4>Client 45</h4>
  </div>
  <div class="filter" data-client="48">
    <h3>Hello World</h3>
    <h4>Client 48</h4>
  </div>
  <div class="filter" data-client="14">
    <h3>Hello World</h3>
    <h4>Client 14</h4>
  </div>
  <div class="filter" data-client="48">
    <h3>Hello World</h3>
    <h4>Client 48</h4>
  </div>
</div>
我也有一把剑

希望有人能帮我过滤掉那些不匹配的选择值的选择

使用
[data client=“YOUR_VALUE”]
选择具有属性的元素
$('.filter[data client=“'+options selected+'“])
将选择所有具有类作为
filter
和属性
data client
的元素作为所选值。[]

.change()
将首先触发
change
事件以筛选所选的值元素

试试这个:

$('#过滤器选择')。更改(函数(){
var optionSelected=$(“选项:selected”,this).val();
$('.filter').hide();
$('.filter[data client=“'+optionSelected+'“]”).show();
}).change()

客户14
客户45
客户48
你好,世界
客户14
你好,世界
客户45
你好,世界
客户48
你好,世界
客户14
你好,世界
客户48
使用
[data client=“YOUR_VALUE”]
选择具有属性的元素
$('.filter[data client=“'+options selected+'“])
将选择所有具有类作为
filter
和属性
data client
的元素作为所选值。[]

.change()
将首先触发
change
事件以筛选所选的值元素

试试这个:

$('#过滤器选择')。更改(函数(){
var optionSelected=$(“选项:selected”,this).val();
$('.filter').hide();
$('.filter[data client=“'+optionSelected+'“]”).show();
}).change()

客户14
客户45
客户48
你好,世界
客户14
你好,世界
客户45
你好,世界
客户48
你好,世界
客户14
你好,世界
客户48
$('#filterSelect').change(function() {
  var optionSelected = $("option:selected", this).val();
  var filter = $('.filter');

  if (filter.attr('data-client') != optionSelected) {
    filter.hide();
  }

});