gpt4 book ai didi

javascript - 仅在客户端根据选择的下拉值过滤城市卡片

转载 作者:行者123 更新时间:2023-11-30 08:17:43 25 4
gpt4 key购买 nike

$(function() {
$('#cityselector').change(function(){
$('.city').hide();
$('#' + $(this).val()).show();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
<Select id="cityselector">
<option selected value="All">All</option>
<option value="Pune">Pune</option>
<option value="Chennai">Chennai</option>
<option value="Bengaluru">Bengaluru</option>
</Select>
</div>
<div class="row">
<div class="col-md-4 city" id="Pune">
<div class="card">
<div class="card-image">

</div>
<div class="card-content">
<span class="card-title">Pune- Koregaon Park</span>
</div>

</div>
</div>
<div class="col-md-4 city" id="Pune">
<div class="card">
<div class="card-image">

</div>
<div class="card-content">
<span class="card-title">Pune- Vimannagar</span>
</div>

</div>
</div>
<div class="col-md-4 city" id="Chennai">
<div class="card">
<div class="card-image">

</div>
<div class="card-content">
<span class="card-title">Chennai</span>
</div>

</div>
</div>

<div class="col-md-4 city" id="Bengaluru">
<div class="card">
<div class="card-image">

</div>
<div class="card-content">
<span class="card-title">Bengaluru</span>
</div>

</div>
</div>

</div>

我正在 shopify 前端构建一个名为商店的新页面。但是,我不会为此使用任何数据库,我只是想根据从下拉列表中选择的城市来过滤或显示/隐藏卡片。我已经使用 jquery show hide 过滤城市。但是当有多个具有相同城市名称的卡片时它不起作用。它只显示一张使用它的城市卡。此外,当用户从下拉列表中选择“全部”选项时,我需要显示所有城市卡片。请帮我解决这个问题。

最佳答案

代替id,给出类名:

$(function() {
$('#cityselector').change(function(){
if($(this).val()=="All"){$('.city').show(); return;}else{$('.city').hide();}
$('.' + $(this).val()).show();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
<Select id="cityselector">
<option selected value="All">All</option>
<option value="Pune">Pune</option>
<option value="Chennai">Chennai</option>
<option value="Bengaluru">Bengaluru</option>
</Select>
</div>
<div class="row">
<div class="col-md-4 city Pune" >
<div class="card">
<div class="card-image">

</div>
<div class="card-content">
<span class="card-title">Pune- Koregaon Park</span>
</div>

</div>
</div>
<div class="col-md-4 city Pune" >
<div class="card">
<div class="card-image">

</div>
<div class="card-content">
<span class="card-title">Pune- Vimannagar</span>
</div>

</div>
</div>
<div class="col-md-4 city Chennai" >
<div class="card">
<div class="card-image">

</div>
<div class="card-content">
<span class="card-title">Chennai</span>
</div>

</div>
</div>

<div class="col-md-4 city Bengaluru" >
<div class="card">
<div class="card-image">

</div>
<div class="card-content">
<span class="card-title">Bengaluru</span>
</div>

</div>
</div>

</div>

关于javascript - 仅在客户端根据选择的下拉值过滤城市卡片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59299314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com