![](/img/trans.png)
[英]Select 2 Multi Select remove option from the dropdown after selected the option
[英]Reset multiple dropdown (select) filters to “All” ( top option) after another option is selected
#filter div包含3个选择:#dpmt_filter,#area_filter和#moi_filter。 我无法使其“重置”其他2个选择框(忽略当前选择框)为“全部”选项(这是顶部选项)。 我以为会是这样的:
$('#filters :input').not($(this)).val('all');
但它没有用。 知道如何到达其他选择框并忽略当前框吗?
jQuery(document).ready(function($){
$('#courses_listing div.accordion').addClass('active');
$('#filter :input').change(function(){
var current_value = $(this).val();
//$('#filters :input').not(current_value).val('all');
if(current_value=="all")
{
$('#courses_listing p').remove('.no-courses');
$('#courses_listing div.accordion').removeClass('hidden').addClass('active');
$('#filters :input').val('all');
}
else
{
if ($('#courses_listing').find('.accordion').hasClass('hidden')){
$('#courses_listing').find('.accordion').not('.' + $(this).val()).removeClass('active').addClass('hidden'); // changes other values from active to hidden.
$('#courses_listing').find('.accordion' + '.' + current_value).removeClass('hidden').addClass('active'); // changes selected value from hidden to active.
} else if ($('#courses_listing').find('.accordion').hasClass('active')){
$('#courses_listing').find('.accordion').not('.' + $(this).val()).removeClass('active').addClass('hidden'); // changes selected value from active to hidden.
}
if (!$('#courses_listing div.accordion').hasClass('active')) {$('#courses_listing div#filters').after('<p class="no-courses">There are no courses with the current filter.</p>');}
else{$('#courses_listing p').remove('.no-courses');}
}});
});
只需添加一下,下面是#filters div中一个选择框的示例:
<div id="filters">
<div id="dpmt_filter">
<p>Select box 1</p>
<form action="<?php bloginfo('url'); echo $_SERVER["REQUEST_URI"]; ?>" method="get">
<?php // Grabs Department terms and displays them
$dpmts = get_terms('departments', array(
'orderby' => 'name',
'hide_empty' => 0
) );
?>
<select id="departments">
<option value="all">All</option>
<?php foreach ($dpmts as $dpmt) : ?>
<option value="<?php echo $dpmt->slug; ?>"><?php echo $dpmt->name; ?></option>
<?php endforeach; ?>
</select>
</form>
</div>
</div>
没有您的标记,我无法真正获得它,但是,由于看到您正在捕获“ current_value”,为什么不将所有输入都设置为“ All”,然后将原始输入设置回去:
$('#filters :input').each(function(e) {
$(this).val('all');
});
$(this).val(current_value);
我确定可以将其修改为使用.not() 。
如果通过写
$('#dpmt_filter:input')。change(function(){
您想将更改侦听器添加到#dpmt_filter select中,这是错误的。 你应该写
$('#dpmt_filter')。change(function(){
因为“ #dpmt_filter:input”选择任何输入,文本区域,#dpmt_filter select中的select或button,所以我认为这不是您的情况。 因此, this
更改监听器不会引用您的#dpmt_filter选择
顺便说一句,您是否尝试调试变更侦听器? 如果曾经调用过该监听器,这将很奇怪,因为这意味着您已经在#dpmt_filter select内放入了一些输入或按钮或文本区域
jQuery没有提供检查我知道的“打开”选择框的功能。
为了将所有选择框设置为第一个,我要做的是:
$('select').find('option:first').prop('selected', 'selected');
我建议在此线程中更多地使用.prop()代替.attr()。 确切地说,在这种情况下,Chrome会给您带来麻烦,但尚未在其他浏览器上对其进行过测试。
我将提出以下建议:
$('#filter select').on('change', function() { // when a new value is selected
$('#filter select') // get all the select boxes
.not(this) // except the one clicked
.find('option:first') // get it's first option
.prop('selected', 'selected'); // mark it as selected
});
我确信可以优化它来访问所有绑定的元素,而不用再次搜索它们。
希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.