2016-12-14 56 views
-4

這裏是我的代碼行之有效here需要幫助縮短這個jQuery函數

在做這樣的事情需要幫助:

$('.date-container input').click(function() { 
    var j; 
    for (j = 1; j <= 4; j++) { 

     $('.day' + (j + 1) + '-times').hide('slow'); 

     $(this).$('.day' + (j + 1) + '-times').show('slow'); 
    } 
}); 
+0

您的代碼似乎並不爲w好吧 - 它仍然顯示我'第2天'項目,當我取消選中該框 –

+1

'j!== i' ......... ??? – Jai

+0

嘗試:'[... Array(4)] .forEach((x,i)=> {/ * code for your loop * /});' –

回答

3

這是你的小提琴JS代碼一個較短的版本:

$(document).ready(function() { 
    // When document is ready, we bind the click event on every 'input' in the '.date-container' div 
    $('.date-container input').click(function(){ 
    // When a click event is triggered on one of those 'input', we hide 
    // all the 'div' that are in the '#checkboxradio' element 
    $('#checkboxradio > div').hide('slow'); 
    // Using the 'id' attribute of the input that triggered the clicked 
    // event (e.g. "day1"), we build a jQuery selector that'll be used 
    // to select the proper checkboxes time container 
    // (e.g. "." + "day1" + "-times" = ".day1-times") 
    // Then we stop all animations on that element (prevent hiding if 
    // it's the element we actually want to display) 
    // and trigger a show animation on that same element 
    $('.' + $(this).prop('id') + '-times').stop().show('slow'); 
    }); 
}); 

你可能想從複選框切換到無線電

+0

對我的情況非常有效。我需要一次選擇多個日期,因此使用了無線電覆選框。感謝你和其他人爲你的時間。 – M1988

+1

我加了評論給我的回答:)。乾杯! – H4ris