确定每个广播组是否都选择了一个选项 [英] Determine if every radio group has had an option selected

查看:69
本文介绍了确定每个广播组是否都选择了一个选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在启用提交按钮之前,我需要确保所有广播组均已应答.如果我有:

I need to make sure that all the radio groups have been answered before I enable the submit button. If I have:

var radioBtns = $('input').filter(':radio');

它告诉我我有多少个单选按钮,但是我需要知道是否有尚未选择选项的组.

It tells me how many radio buttons I've got, but I need to know if there are any groups that haven't had an option selected yet.

推荐答案

如果您知道有多少组,就可以这样做:

If you know how many groups you have you can just do:

if($('input:radio:checked').length < numGroups){
    // At least one group isn't checked
}

否则,您需要首先计算组数.我想不出什么办法可以做到这一点:

Otherwise you need to count the number of groups first. I can't think of any way to do this better then:

var rgroups = [];
$('input:radio').each(function(index, el){
        var i;
        for(i = 0; i < rgroups.length; i++)
            if(rgroups[i] == $(el).attr('name'))
                return true;
        rgroups.push($(el).attr('name'));
    }
);
rgroups = rgroups.length;

if($('input:radio:checked').length < rgroups)
    alert('You must fill in all the fields.');
else
    alert('Thanks!');

这篇关于确定每个广播组是否都选择了一个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆