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(); 
     } 
});