2013-07-25 117 views
0

存在#filter div,其中包含#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> 

回答

0

没有您的标记,我真的不能得到它,但是因为我看你”重新捕获“current_value”,为什么不将所有的输入为“全部”,然后设置原始回:

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

我敢肯定,这可以被修改为使用。不是()

+0

你现在能更好地理解它吗?对不起,我更新了代码以使其更有意义。 –

0

如果通过写

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

你想添加更改侦听到#dpmt_filter选择,这是错误的。 你应该写

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

因为“#dpmt_filter:input”选择任何输入,textarea,选择或按钮在你的#dpmt_filter选择,这是不是你的情况,我认为。因此,this在更改监听器does not引用您的#dpmt_filter选择

顺便说一下,您是否试图调试您的更改侦听器?这听起来很奇怪,因为这意味着你已经在#dpmt_filter中加入了一些输入或者按钮或者textarea select

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

我敢肯定,这可以优化其访问所有绑定的元素再次为他们寻找代替。

希望它有帮助!

相关问题