0

すべての兄弟(ラジオボタングループのラベル)から属性'rel'の値を配列に取得する方法を知っている人はいますか?

私はこのようなことを試しましたが、明らかに機能しません:

<label rel="option_local" class="type_radio" for="type_3">
    <input type="radio" checked="checked" value="3" id="type_3" name="type" /> 
Local</label>
<label rel="option_remote" class="type_radio" for="type_2">
    <input type="radio" value="2" id="type_2" name="type" /> 
Remote</label>
<label rel="option_html" class="type_radio" for="type_1">
    <input type="radio" value="1" id="type_1" name="type" /> 
HTML</label>


$('.type_radio').click(function() {
    var r = $(this).attr('rel');
    var arr = $(this).siblings().attr('rel');   
    $.each(arr, function(k, v) {
        $('.' + v).addClass('dn');
    });
    $('.' + r).removeClass('dn');
    return false;
});

特定のトリガーの「rel」属性を参照し、それらから特定のクラスを削除するクラスを持つ要素をループしようとしています。次に、このクラスを、選択したクラスを参照するクラスに適用します。

すべてのラベルにはクラス'type_radio'があります。

4

1 に答える 1

1

わかりました-なんとか達成しました-方法は次のとおりです。

$('.type_radio').click(function() {
    var r = $(this).attr('rel');
    var arr = $(this).siblings();   
    var tr;
    $.each(arr, function() {
        tr = $(this).attr('rel');
        $('.' + tr).addClass('dn');
    });
    $('.' + r).removeClass('dn');
});
于 2011-06-05T11:58:59.243 に答える