【问题标题】:How can I get all the radio buttons in each group and check one is checked in each group?如何获取每组中的所有单选按钮并检查每组中是否选中一个?
【发布时间】:2012-10-04 14:13:12
【问题描述】:

表单中的单选按钮范围。 jQuery 检查是否为每个组选择了一个单选,如果没有,则向关联的标签添加一个类。

我已检查并且组的名称已正确添加到变量“radio”中。有什么想法吗?

$('.checkbox', this).each(function()
{
    var radio = $(this).attr("name");
    if ($('input[name='+ radio +']:checked').length) 
    {
        $('label[for=' + radio + ']').addClass('error_label');
        required = false;
    }
    else 
    {
        $('label[for=' + radio + ']').removeClass('error_label');
    }
});

【问题讨论】:

  • 当您选中其中一个单选按钮时,您正在添加 error_label 类,而不是您的问题似乎暗示的相反。

标签: javascript jquery radio-button radio-group


【解决方案1】:

我认为你应该在添加类时这样检查:

if ($('input[name=' + radio + ']:radio:checked').length == 0) {
    // Add class...
}

【讨论】:

    【解决方案2】:

    这是一种更有效的方法。此外,您可能不想将“错误”类添加到组内的所有单选按钮标签。

    Please check this

    === 已更新以在答案中包含代码 ===

    HTML

    <div class="radio-group">
        <h3>Group 1</h3>
        <input type="radio" name="group_1" id="opt1" value="1" />
        <label for="opt1">Option 1</label>
        <br />
        <input type="radio" name="group_1" id="opt2" value="2" />
        <label for="opt2">Option 2</label>
        <br />
        <input type="radio" name="group_1" id="opt3" value="3" />
        <label for="opt3">Option 3</label>
        <br />
    </div>
    <div class="radio-group">
        <h3>Group 2</h3>
        <input type="radio" name="group_2" id="opt4" value="1" />
        <label for="opt4">Option 1</label>
        <br />
        <input type="radio" name="group_2" id="opt5" value="2" />
        <label for="opt5">Option 2</label>
        <br />
        <input type="radio" name="group_2" id="opt6" value="3" />
        <label for="opt6">Option 3</label>
    </div>
    <div class="radio-group">
        <h3>Group 3</h3>
        <input type="radio" name="group_3" id="opt8" value="1" />
        <label for="opt8">Option 1</label>
        <br />
        <input type="radio" name="group_3" id="opt9" value="2" />
        <label for="opt9">Option 2</label>
        <br />
        <input type="radio" name="group_3" id="opt10" value="3" />
        <label for="opt10">Option 3</label>
    </div>
    <div>
        <input type="button" id="submit" value="Submit" />
    </div>
    

    JavaScript

    $(function(){
        $("#submit").click(function(){
            $(".radio-group").each(function(){
                if($(this).find("input:radio:checked").length) {
                    $(this).removeClass("error");
                }
                else {
                    $(this).addClass("error");
                }
            });
        });
    });​
    

    【讨论】:

    • 在检查丢失的收音机时使用 removeClass() 也会有所帮助,因此用户在再次尝试提交之前会看到所有错误都已清除
    • 小提琴很好,但也请在您的答案中发布代码。 Stack Overflow 的目标是自包含,很难讨论实际上不在这里的代码(例如,@charlietfl 上面的评论没有可见的上下文可供构建)。
    • @FrédéricHamidi 会注意这一点。感谢您指出。
    猜你喜欢
    • 1970-01-01
    • 2017-07-24
    • 2011-04-02
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    相关资源
    最近更新 更多