简体   繁体   English

选择另一个选项后,将多个下拉(选择)过滤器重置为“全部”(顶部选项)

[英]Reset multiple dropdown (select) filters to “All” ( top option) after another option is selected

There is a #filter div which holds the 3 selects: #dpmt_filter, #area_filter and #moi_filter. #filter div包含3个选择:#dpmt_filter,#area_filter和#moi_filter。 I cannot get it to "reset" the other 2 select boxes (ignoring current select box) to the "All" option (which is the top option). 我无法使其“重置”其他2个选择框(忽略当前选择框)为“全部”选项(这是顶部选项)。 I thought it would be something like: 我以为会是这样的:

$('#filters :input').not($(this)).val('all');

but it did not work. 但它没有用。 Any idea how I can reach the other select boxes and ignore the current? 知道如何到达其他选择框并忽略当前框吗?

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');}
  }});
});

Just to add, here is an example of one select box inside the #filters div: 只需添加一下,下面是#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>

Without your markup, I can't really get it, however since I see you're capturing 'current_value', why not just set all inputs to 'All' and THEN set the original back: 没有您的标记,我无法真正获得它,但是,由于看到您正在捕获“ current_value”,为什么不将所有输入都设置为“ All”,然后将原始输入设置回去:

$('#filters :input').each(function(e) {
    $(this).val('all');
});
$(this).val(current_value);

I'm sure this can be modified to use .not() . 我确定可以将其修改为使用.not()

If by writing 如果通过写

$('#dpmt_filter :input').change(function(){ $('#dpmt_filter:input')。change(function(){

you wanted to add change listener to #dpmt_filter select, it is wrong. 您想将更改侦听器添加到#dpmt_filter select中,这是错误的。 You should write 你应该写

$('#dpmt_filter').change(function(){ $('#dpmt_filter')。change(function(){

Because "#dpmt_filter :input" selects any input, textarea, select or button inside your #dpmt_filter select, which is not your case I think. 因为“ #dpmt_filter:input”选择任何输入,文本区域,#dpmt_filter select中的select或button,所以我认为这不是您的情况。 Therefore, this in change listener doesnt refer to your #dpmt_filter select 因此, this更改监听器不会引用您的#dpmt_filter选择

By the way, did you tried to debug your change listener? 顺便说一句,您是否尝试调试变更侦听器? It will be strange if that listener ever gets called, because it means that you have put some input or button or textarea inside your #dpmt_filter select 如果曾经调用过该监听器,这将很奇怪,因为这意味着您已经在#dpmt_filter select内放入了一些输入或按钮或文本区域

There is no functionality provided by jQuery to check for "open" select boxes, that I know of. jQuery没有提供检查我知道的“打开”选择框的功能。

In order to set all the select boxes to the first one I would do: 为了将所有选择框设置为第一个,我要做的是:

$('select').find('option:first').prop('selected', 'selected');

I recommend using .prop() in stead of .attr(), more in this thread . 我建议在此线程中更多地使用.prop()代替.attr()。 To be precise, in this case Chrome will give you trouble, haven't tested it on other browsers. 确切地说,在这种情况下,Chrome会给您带来麻烦,但尚未在其他浏览器上对其进行过测试。

I would propose the following: 我将提出以下建议:

$('#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
});

I'm sure it's possible to optimize it accessing all the binded elements in stead of searching for them again. 我确信可以优化它来访问所有绑定的元素,而不用再次搜索它们。

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM