RadioButton Group - Element not in array when no value selected

RadioButton Group - Element not in array when no value selected

Hi All,

I'm trying to put together some form validation for a survey form - The number of questions are unknown, so I'm trying to get an array of all the question fields, then check their value. All the question fields are RadioButtons.

I've hit a problem where my selector only gets the element if a value has been selected - If a value has not been selected the element is ignored so is not in the array, which means during the Loop my code cannot check that the question in the survey has been answered.

I've put together a simple page which demonstrates the problem, included below. What I would like is for the alert() on line 9 to happen whether or not an item has been selected for each radiobutton group.

Any help/advice much appreciated.

Thank you! :o)

Simon.

  1. <html>
  2.     <head>
  3.     <title>jQuery Radio Button Selector Test</title>
  4.     <script type="text/javascript" src="jquery-1.4.1-vsdoc.js"></script>
  5.     <script>
  6.         function AlertRadioButtons() {
  7.             var surveyFields = $("[name*='QU-']").serializeArray();
  8.             jQuery.each(surveyFields, function(i, field) {
  9.                alert(field.name + ": " + field.value);
                    })
                }
        </script>
        </head>
        <body>
            
            1<input id="QU-SV_Q9_0" type="radio" name="QU-SV_Q9" value="1" /><br />
            2<input id="QU-SV_Q9_1" type="radio" name="QU-SV_Q9" value="2" /><br />
            3<input id="QU-SV_Q9_2" type="radio" name="QU-SV_Q9" value="3" /><br />
            4<input id="QU-SV_Q9_3" type="radio" name="QU-SV_Q9" value="4" /><br />
            5<input id="QU-SV_Q9_4" type="radio" name="QU-SV_Q9" value="5" /><br />
            empty<input id="QU-SV_Q9_5" type="radio" name="QU-SV_Q9" value="" /><br />
            
            1a<input id="QU-SV_Q10_0" type="radio" name="QU-SV_Q10" value="1a" /><br />
            2a<input id="QU-SV_Q10_1" type="radio" name="QU-SV_Q10" value="2a" /><br />
            3a<input id="QU-SV_Q10_2" type="radio" name="QU-SV_Q10" value="3a" /><br />
            4a<input id="QU-SV_Q10_3" type="radio" name="QU-SV_Q10" value="4a" /><br />
            5a<input id="QU-SV_Q10_4" type="radio" name="QU-SV_Q10" value="5a" /><br />
            empty<input id="QU-SV_Q10_5" type="radio" name="QU-SV_Q10" value="" /><br />
            
            <input type="button" value="Click Me" onclick="AlertRadioButtons();" />
        </body>
    </html>