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

时间:2013-07-25 21:48:26

标签: javascript jquery html wordpress forms

有一个#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>

3 个答案:

答案 0 :(得分:0)

没有你的标记,我无法得到它,但是因为我看到你正在捕获'current_value',为什么不将所有输入设置为'All'然后将原始设置为:

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

我确信可以将其修改为使用 .not()

答案 1 :(得分:0)

如果写作

  

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

你想将更改监听器添加到#dpmt_filter select,这是错误的。 你应该写

  

$( '#dpmt_filter')。改变(函数(){

因为“#dpmt_filter:input”选择了#dpmt_filter select中的任何输入,textarea,select或button,我认为这不是你的情况。因此,更改侦听器中的this不会引用您的#dpmt_filter select

顺便问一下,您是否尝试调试更改侦听器?如果那个监听器被调用会很奇怪,因为这意味着你已经在#dpmt_filter select中放入了一些输入或按钮或textarea

答案 2 :(得分:0)

jQuery没有提供检查“打开”选择框的功能,我知道。

为了将所有选择框设置为第一个,我会这样做:

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

我建议使用.prop()代替.attr(),更多地使用this thread。确切地说,在这种情况下,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
});

我确信可以优化它访问所有绑定元素,而不是再次搜索它们。

希望它有所帮助!