如何让jquery脚本在下拉菜单事件中启动?
对于过滤器搜索,我使用一个表单来显示一个下拉菜单,我需要根据从菜单中选择的选项的值向父div添加或删除一个类 此脚本至少有两个问题: 1.它正在加载页面并立即添加类。我需要它只在用户选择选项时运行。 2.选择选项不会更新页面。 3.另外,我不确定我的变量$key是否存储了正确的值。我想要从选项中选择的值 我已经研究了几个如此相似的问题,并查看了.select、.change和.trigger的jquery文档,但我还没有将适合我的情况的组合放在一起 下面是我正在使用的精简基本脚本,它有效地将类添加到示例HTML中如何让jquery脚本在下拉菜单事件中启动?,jquery,scripting,Jquery,Scripting,对于过滤器搜索,我使用一个表单来显示一个下拉菜单,我需要根据从菜单中选择的选项的值向父div添加或删除一个类 此脚本至少有两个问题: 1.它正在加载页面并立即添加类。我需要它只在用户选择选项时运行。 2.选择选项不会更新页面。 3.另外,我不确定我的变量$key是否存储了正确的值。我想要从选项中选择的值 我已经研究了几个如此相似的问题,并查看了.select、.change和.trigger的jquery文档,但我还没有将适合我的情况的组合放在一起 下面是我正在使用的精简基本脚本,它有效地将类添
<select name="SelectBox-ByDestination" id="SelectBox-ByDestination">
<option value="" disabled selected hidden>Select a Destination...</option>
<option value="Argentina">Argentina</option>
<option value="Australia">Australia</option>
<option value="Iceland">Iceland</option>
</select>
<div class="Item">
<h3>Program</h3>
<div class="destination">
<h4>Destinations</h4>
<ul>
<li>Iceland</li>
</ul>
</div>
</div>
$(function () {
$('#SelectBox-ByDestination').change(function() {
var $dest = $(this).val();
$('.destinations').each(function() {
var $matches = $('.destination li').filter(function() {
return $(this).text() === $dest
});
if (!$matches.length) {
$(this).parent().addClass('hideByDestinationDropDownMenu');
} else {
$(this).parent().removeClass('hideByDestinationDropDownMenu');
}
});
});
});
您需要的第一件事是在
然后,您希望在事件触发时将其值与单个元素中的文本进行比较
$('#SelectBox-ByDestination').on('change', function() {
var dest = $(this).val();
$('.destinations').each(function() {
// filter matching `<li>`
var $matches = $('.destintion li').filter(function() {
return $(this).text() === dest// or use `includes() for partial matches
});
if(!$matches.length){
// hide this destination div
}else{
// show it
}
});
});
我猜您也想隐藏其他问题,但这应该可以让您开始charlietfl我修改了脚本以利用您的建议,他们修复了我列出的三个问题中的两个。但当用户在下拉菜单上操作时,它仍然不会启动。