简体   繁体   English

jquery:如何检查是否选中了div中的所有单选按钮

[英]jquery: how to check if all radio buttons in a div are selected

my html looks like this 我的HTML看起来像这样

<div id="div1">
  <input type="radio" name="r1" value="v1" />
  <input type="radio" name="r1" value="v2" />
  <input type="radio" name="r1" value="v3" />

  <input type="radio" name="r2" value="v1" />
  <input type="radio" name="r2" value="v2" />
  <input type="radio" name="r2" value="v3" />

  <input type="radio" name="r3" value="v1" />
  <input type="radio" name="r3" value="v2" />
  <input type="radio" name="r3" value="v3" />
</div>

radio buttons are dynamically generated on my html so in that div i don't know how many radio buttons i have. 单选按钮是在我的html上动态生成的,所以在div中我不知道我有多少个单选按钮。

i want to make sure that the user will select a value for each one of them before he submits the form, how can i check that all radio buttons inside my div has a value checked? 我想确保用户在提交表单之前为每一个选择一个值,如何检查我的div中的所有单选按钮是否都检查了值?

Thank you 谢谢

$(":radio").change(function() {
    var names = {};
    $(':radio').each(function() {
        names[$(this).attr('name')] = true;
    });
    var count = 0;
    $.each(names, function() { 
        count++;
    });
    if ($(':radio:checked').length === count) {
        alert("all answered");
    }
}).change();

Demo: http://jsfiddle.net/yFaAj/15/ 演示: http//jsfiddle.net/yFaAj/15/

Restructure your HTML slightly - wrap each radio group in (say) a div. 稍微重构您的HTML - 将每个广播组包装在(例如)div中。 Then you can just do something like this to validate the form when the user submits it: 然后,当用户提交表单时,您可以执行以下操作来验证表单:

if ($('div:not(:has(:radio:checked))').length) {
    alert("At least one group is blank");
}

Of course this can be tweaked in various ways to suit your HTML. 当然,可以通过各种方式调整以适应您的HTML。 The idea was from Find all radio groups which haven't been selected 这个想法来自查找尚未被选中的所有无线电组

Solution here http://jsfiddle.net/QxdnZ/1/ 解决方案 http://jsfiddle.net/QxdnZ/1/

    var checked = $("#div1 :radio:checked");
    var groups = [];
    $("#div1 :radio").each(function() {
        if (groups.indexOf(this.name) < 0) {
            groups.push(this.name);
        }
    });
    if (groups.length == checked.length) {
        alert('all are checked!');
    }
    else {
        var total = groups.length - checked.length;
        var a = total>1?' groups are ':' group is ';

        alert(total + a + 'not selected');
    }

Looking for something along these lines? 在这些方面寻找一些东西? http://jsfiddle.net/gXsZp/3/ http://jsfiddle.net/gXsZp/3/

<div id="div1">
   Q1
  <input type="radio" name="r1" value="v1" />
  <input type="radio" name="r1" value="v2" />
  <input type="radio" name="r1" value="v3" />
  <br/>Q2

  <input type="radio" name="r2" value="v1" />
  <input type="radio" name="r2" value="v2" />
  <input type="radio" name="r2" value="v3" />
    <br/>Q3  

  <input type="radio" name="r3" value="v1" />
  <input type="radio" name="r3" value="v2" />
  <input type="radio" name="r3" value="v3" />
</div>
<br/>
<input id="btn" type="submit" text="submit"/>



$('#btn').click(function(){
    if ( $('#div1 input:radio:checked').size() == 3 )    
        return true;
    return false;
});

Validate the form when the user submits it, using this validation code. 使用此验证代码在用户提交表单时验证表单。

var blank = false;
$("input:radio").each(function() {
    var val = $('input:radio[name=' + this.name + ']:checked').val();
    if (val === undefined) {
        blank = true;
        return false;
    }
});
alert(blank ? "At least one group is blank" : "All groups are checked");

First we get the names of all the radio button groups, then check that each one has a value. 首先,我们获取所有单选按钮组的名称,然后检查每个单选按钮组是否有值。 (Actually we're doing multiple checks, but that doesn't really matter.) (实际上我们正在做多次检查,但这并不重要。)

Try this: 试试这个:

function check(){
    var allCheck = true;
    if($("#div1 :radio:checked").length==0){
        allCheck=false;
    }
    $("#div1 :radio").each(function(){
    for(var i=0;i<$("#div1 :radio:checked").length;i++)
    if($(this).attr("name")===$($("#div1 :radio:checked")[i]).attr("name")) 
                break;
    else if(i==$("#div1 :radio:checked").length-1) 
                allCheck = false;
    });

    return allCheck;
}

Try this one: 试试这个:

$('input:radio', $('#div1')).each(function() {
    if(name && name == $(this).attr('name'))
        return true; // Skip when checking the same element name. 

    name = $(this).attr('name');

    if(! $('input:radio[name="' + name + '"]:checked').length) {
        alert('Oops, you missed some input there.. [' + name + ']');
        return false;
    }
});

It will loop through every radio button to check for checked radio & will break as soon it found non-checked radio group (first error found). 它将遍历每个单选按钮以检查已检查的无线电,并在找到未检查的无线电组时立即中断(发现第一个错误)。 But if you prefer to get all the errors (not only the first error found), just remove return false . 但是如果您希望得到所有错误(不仅是找到的第一个错误),只需删除return false

This will work: 这将有效:

if($("#div1").children("input:radio:checked").size() == 3) 
{
   alert('three inputs were checked'); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM