2013-04-24 32 views
1

我已經在這裏工作的例子:http://jsfiddle.net/infatti/esLMh/切換收音機顯示/隱藏只有最接近

爲什麼「不」不觸發隱藏?

$('.hide-show input').change(function() { 
    $(this).closest('.hide-show').next('.hide-show-yes').toggle(this.value == 'yes'); 
    $(this).closest('.hide-show').next('.hide-show-no').toggle(this.value == 'no'); 
}); 
$('.hide-show input:checked').change(); //trigger correct state onload 

回答

1

next只選擇所選元素(如果指定的選擇匹配),根據您的標記,你應該叫選擇第二目標元素2種next方法的下一個緊接其後。

$('.hide-show input').change(function() { 
    $(this).closest('.hide-show') 
      .next('.hide-show-yes').toggle(this.value == 'yes') 
      .next('.hide-show-no').toggle(this.value == 'no'); 
}); 

http://jsfiddle.net/A8ycG/

您還可以使用nextAll的方法,但這種方法是矯枉過正在這種情況下。