【问题标题】:Assign "selected" to radio label with jquery使用 jquery 将“选定”分配给单选标签
【发布时间】:2013-08-11 19:22:36
【问题描述】:

我有一组带有相关标签的无线电输入。有一些 CSS 魔法可以美化按钮,这很好用。我需要能够更改标签的选定属性。

<input type='radio' id='radio1' name='resolution' value='0' selected />
<input type='radio' id='radio2' name='resolution' value='1' />
<label for='radio1' class='cb-enable selected' ><span>Open</span></label>
<label for='radio2' class='cb-disable ' ><span>Closed</span></label>

如何使用 jQuery 设置标签选择器?

【问题讨论】:

    标签: jquery label radio selected


    【解决方案1】:
    $("input:radio[name='resolution']").change(function(){
        $("label").removeClass("selected");
        $("label[for='" + this.id + "']").toggleClass("selected", this.checked);
    });
    

    http://jsfiddle.net/QKL5t/

    【讨论】:

    • 感谢您的更新。我正在寻找removeClass 和toggleClass。这有效:$('label[for=radio1]').removeClass(\"selected\");$('label[for=radio2]').toggleClass(\"selected\", this.checked);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多