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
4

1 に答える 1

1

nextマークアップに基づいて、選択した要素の次の直接の兄弟のみを選択します (指定されたセレクターに一致する場合next)。2番目のターゲット要素を選択するには、2 つのメソッドを呼び出す必要があります。

$('.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、この場合、このメソッドはやり過ぎです。

于 2013-04-24T16:35:53.247 に答える