1

I've got an annoyingly long list of radio button question / answers

Do you like A or B
Do you prefer x or y

Those type of questions.

Markup looks a little something like:

<input type="radio" name="User[AnnoyingQuestion][1]" value="1|Do you like x or y?|X" id="Member[PartyProfile]_1_0" />

<input type="radio" name="User[AnnoyingQuestion][1]" value="1|Do you like x or y?|Y" id="Member[PartyProfile]_1_1" class="elp-radio-question">

Basically, I need to ensure that all of the questions have been answered. What's a good strategy for doing this with jQuery?

Alex
  • 37,502
  • 51
  • 204
  • 332

4 Answers4

4

Proof: http://jsfiddle.net/iambriansreed/vXXUV/

Set the form selector to be more exact.

<script>

    $(function(){

        $('form').submit(function(e){

            var radios = {}, success = true;

            $('input[type="radio"]').each(function(){

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

                 if(name in radios) return true;                 

                 radios[name] = $('input[name="'+name+'"]:checked').length > 0;

            });    

            for(name in radios){
                if(!radios[name]){
                    success = false;
                    alert('Please enter a value for '+name);
                }
            }

            if(!success) e.preventDefault();

            else alert('Form Submitting...');

        });        

    });​

</script>
iambriansreed
  • 21,935
  • 6
  • 63
  • 79
1

You should be able to use the jQuery validation plugin, as proposed in this question:

Validation of radio button group using jQuery validation plugin

Here are the validation plugin docs: http://docs.jquery.com/Plugins/Validation/Methods

Community
  • 1
  • 1
mltsy
  • 6,598
  • 3
  • 38
  • 51
1

UPDATE. try this

function allChecked() {
    var $inputs = $('input[type="radio"]');

    $.each($inputs,function(){
        var $this = $(this),
        name = $this.attr('name');

        if ($('input[name="' + name +'"]:checked').length < 1){
            return false;
        }
    });

    return true;
}

this isn't incredibly efficient but i think it will work

Evan
  • 5,975
  • 8
  • 34
  • 63
0

Here's a quick way to do it assuming all your questions have just two options. Compare the number of checked radio buttons to the total number of radio button. If the number of checked boxes doesn't equal half the total, then something wasn't checked.

if( $('input[type=radio]:checked').length) == $('input[type=radio]').length/2) ...​​​​​​​​​​​​​​​​​​​
j08691
  • 204,283
  • 31
  • 260
  • 272