2017-02-22 94 views
-1

我想使用JQuery根據選定div的值更改複選框的顯示。具體而言,如果在facetwp-facet-city下拉列表中選擇了「Berlin」,我想隱藏「Neukölln」和「Mitte」複選框。我知道這是可能的使用JQuery,但不幸的是我的技能還不是那個水平(還)。任何幫助將非常感激!在截圖中,我突出顯示了「柏林」值和Mitte複選框。JQuery根據下拉列表隱藏複選框

截圖的網址爲:https://www.wgeil.de/listings/?fwp_city=berlin Screenshot

+0

您嘗試了哪些解決方案?向我們展示您的代碼,並且有人可能會幫助您爲您進行調試。 –

+0

我試過這段代碼(史提夫),但它不工作。任何想法爲什麼不呢? ('(change)',function(e){if($(this).val()=='berlin'){(document.ready){function(){('。facetwp-dropdown')。 )($(this).data(「value」)=='mitte') $(this).hide() ); } 如果($(本)。數據( 「值」)== '克爾恩'){$ (本).hide();} } ) }) }); – Ron

+0

發佈實際代碼而不是截圖或製作小提琴。這對我們來說會更容易幫助。 – mhshimul

回答

0

嘗試是這樣的:

$(document).ready(function() { 
    $('.facetwp-dropdown').on('change', function(e) { 
    if ($(this).val() == 'berlin') 
     $('.facetwp-checkbox').each(function(i, v) { 
     if ($(this).data("value") == 'mitte') { 
      $(this).hide(); 
     } 
     if ($(this).data("value") == 'neukolln') { 
      $(this).hide(); 
     } 
     }) 
    }) 
}); 
+0

謝謝,我會給這個鏡頭! – Ron

+0

不幸的是,它似乎無法工作! – Ron

+0

你在你的HTML中包含了jQuery嗎?你能創造一個小提琴嗎?這將更容易找出問題。 – mhshimul

0

以一定的啓發市的代碼,這裏是一個可能的解決方案。爲了幫助你繼續前進,如果某些東西不起作用,你可能想看看它是否會在控制檯中丟失錯誤(然後嘗試並調試它們),或者添加一些console.log(/* some variable */);語句來列出某些選擇器返回的內容,這可能會讓你弄清楚事情失敗的地方(或者至少讓你指出一個特定的問題)。

// formatting for readability. 
$(document).ready(function() { 
    // Should trigger callback function on change in dropdown. 
    $('.facetwp-dropdown').on('change', function(e) { 

    // Should set the value of the selected dropdown option. 
    let selectedOption = $(this).find(':selected').prop('value'); 

    // Conditional if option equals berlin. 
    if (selectedOption == 'berlin') { 
     // Grab all checkboxes using their parent div. 
     let checkboxes = $('.facet-wrapper').children(); 

     // Use jQuery.each method to iterate over the child elements. 
     $.each(checkboxes, function(index, value) { 
     // Set value of the data attribute. 
     let attribute = $(value).attr('data-value'); 

     // Check data attribute value against either option. 
     if (attribute == 'neukolln' || attribute == 'mitte') { 
      // Hide div if conditional evaluates true. 
      $(value).hide(); 
     } 
     }); 
    }  
    }); 
}); 
+0

謝謝,我會試試這個! – Ron

+0

https://jsfiddle.net/Lkt613yf/ – Ron

+0

你的第一個問題是你將'selectedOption'設置爲等於你的選擇器,但是在你的條件中試着對'selectOption'進行測試:)(在我的代碼示例中這樣做了,對不起對於錯字)。一旦你修復隱藏在jquery中的東西就像'$('/ *你的選擇器* /')一樣簡單。hide();' –