checking at least one radio button is selected from each group (with jquery)

Viewed 41042

I'm dynamically pulling and displaying radio button quesitons on a form (separated into different sections by jquery tabs).

So there are lots of 2, 3, 4, 5 etc radio buttons together, which have the same 'name' but different ids.

When you hit next between the sections, I want to check that at least one radio button has been selected for each group.

( When the form loads, the radio buttons have none checked by default and it has to be this way. )

I can loop through the current section's radio buttons but I'm not sure how to easily check that one has been checked for each group?

6 Answers

Input names must be same name: sendType

Check this out:

$('#submitBtn').click(function(e){

   alert ($('input:radio[name="sendType"]:checked').length);

   if ($('input:radio[name="sendType"]:checked').length > 0) {
       //GO AHEAD

   } else {
       alert("SELECT ONE!");
       e.preventDefault();
   }

});

use this code

remember to put button outside the form

$("#buttonid").click(function(){
                    var check = true;
                    $("input:radio").each(function(){
                        var name= $(this).attr('name');
                        if($("input:radio[name="+name+"]:checked").length){
                            check = true;
                        }
                        else{
                            check=false;
                        }
                    });

                    if(check){
                        $("#formid").submit();
                    }else{
                        alert('Please select at least one answer in each question.');
                    }
                });
Related