2012-07-14 68 views
0

我有重複的元素顯示下一類元素

 <input style="display:inline" class="chkClass" type="checkbox" value="1"/> 
    <div class="other"> 
//something 
    </div> 

    <input style="display:inline" class="chkClass" type="checkbox" value="1"/> 
    <div class="other"> 
//something 
    </div> 
    .... 

隨着我只想顯示下一個「其他」類複選框的變化。它不`噸的工作:

 $('.other').hide(); //hide all "other" elements 

     $('.chkClass').live('change', function(){ 
      if($(this).is(':checked')){ 
       $(this).next('.other').show(); 
     } 
     }); 
+0

另一個$(」。其他')。hide()應該在功能 – 2012-07-14 19:19:45

+1

裏面,你想隱藏並顯示在支票上,或者只是顯示,如果不勾選,就不打擾隱藏?因爲你的代碼應該工作得很好,如果你不想隱藏,如果沒有選中,除非你得到任何js錯誤。 – Kishore 2012-07-14 19:23:11

+0

定義「不起作用」。它的作品[這裏](http://jsfiddle.net/fUHXz/) – Engineer 2012-07-14 19:24:50

回答

0

我得到它,但我需要爲每個元素設置ID:

$('.allOther').hide(); 


    $('.chkClass').on('change', function(){ 
      if($(this).is(':checked')){ 
       var array = $(this).attr('id').split('_'); 
       $('#other'+array[1]).show("slow"); 

      } 
     }); 

     <input style="display:inline" class="chkClass" id="smthng_<?php echo $counter ?>" type="checkbox" value="1"/> 
    <div id ="other<?php echo $counter ?>" class="allOther"> 
0

使用nextAll與查詢

$('.chkClass').on('change', function(){ 
     if($(this).is(':checked')){ 
      $(this).nextAll('.other:hidden:first').show(); 
     } 
});