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

前端 未结 8 1114
一向
一向 2021-02-07 02:13

my html looks like this

8条回答
  •  攒了一身酷
    2021-02-07 02:52

    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.

提交回复
热议问题