1

なぜこれが機能しないのですか?ラベルを"$(this).toggleClass('checked');"オンにalert('test')してクリックすると、警告ウィンドウが表示されます:-/

$(document).ready(function(){
    $("label").click(function(){
        $(this).toggleClass('checked');
    });
});

<input type="radio" name="hours" id="hours1" value="xyz" style="display: none" />
<label for="hours1">10:00</label>
<input type="radio" name="hours" id="hours2" value="xyz" style="display: none" />
<label for="hours2">11:00</label>
4

2 に答える 2

1

あなたがしたいことは、現在選択されているチェックボックスのラベルに「チェック済み」クラスを持たせることであると仮定します。

http://jsfiddle.net/4pQt8/3/

$("label")注:現在のように、ドキュメント内のすべてのラベルからチェックされたクラスが削除されるため、セレクターを調整する必要があります。

$(document).ready(function(){
    $("input[name=\"hours\"]").change(function(){
        if ($(this).attr("checked")) {
            $("label").removeClass("checked");
            $("label[for=\"" + $(this).attr("id") + "\"]").addClass("checked");
        }
    });
});

</p>

于 2012-06-03T18:59:29.060 に答える
1

この作業コードを参照してください。私はそれがあなたのニーズを満たすことを願っています

$(document).ready(function(){
    $("label").click(function(){
        if(!$(this).hasClass('checked')){
            $(this).addClass('checked');
        }else{
               $(this).toggleClass('checked');
        }
    });
});

<input type="radio" name="hours" id="hours1" value="xyz" style="display: block" />
<label for="hours1">10:00</label>
<input type="radio" name="hours" id="hours2" value="xyz" style="display: block" />
<label for="hours2">11:00</label>
于 2012-06-03T18:30:21.007 に答える