2013-07-25 57 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 
}); 

我敢肯定,這可以優化其訪問所有綁定的元素再次爲他們尋找代替。

希望它有幫助!

相關問題