2016-12-14 2 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

から無線に切り替えたいですオークはそれをよく捉えています - それでも私はその日の2日目のアイテムを表示します。 –

+1

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

+0

次のようにしてください: '[... Array(4)]。forEach((x、i)=> {/ *あなたのforループ内のコード* /}); –

答えて

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

関連する問題