Question

I am fairly new to jquery and I am working with jsp and struts .as part of the application I have a survey form with a number of categories each with different set of questions, and each question with a number of possible answers to pick from as radio buttons. I want the user to be required to pick one answer for each question in each category before submitting the form and to give an error if they don't.

<s:iterator value="quelists">
<tr class="radioList">
<td>
<s:property  value="surveyQuestion"/><span id="msg_selectError"/> 
</td>

<td >
<s:iterator value="answerslists" status="status">
<s:radio  value="selectedAnswers[%{#count}]"  
          name="selectedAnswers[%{#count}]"  
          list="#{id:answer}"  required="true" theme="simple"/>
</s:iterator>
</td>

<s:set var="count" value="#count+1"/>    
</tr>
</s:iterator> 
Was it helpful?

Solution

I am just learning.. It work: http://jsfiddle.net/WDfXq/2/

function isCheck() {
    var isOk = true;
    $('.radioList').each(function(){
         var countChecked = $(this).find(':checked').length;

         if(countChecked!=1) {
             $(this).css('color', 'red');
             isOk = false;
         } else {
             $(this).css('color', 'black');
         }
    }); 
    return isOk;
}

OTHER TIPS

i managed to do it thru the following:

the Struts part

<tbody>
<s:iterator value="quelists">
<tr class="radioList">
<td align="right" dir="rtl"><s:property  value="surveyQuestion"/></td>
<td >
<div class="controlsetRadio"> 
<s:iterator value="answerslists" status="status">
<s:radio id="selectedAnswers[%{#count},%{#status.count}]" 
         value="selectedAnswers[%{#count}]"  
         name="selectedAnswers[%{#count}]"   
         list="#{id:answer}"  
         required="true" theme="simple"/>
</s:iterator>
</div>
</td>
<s:set var="count" value="#count+1"/>
</tr>
</s:iterator>
</tbody>

the JQuery part

  <script type="text/javascript">

    $(document).ready(function(){

      $("form").submit(function(){
        var validateSurvey = Checkform();        
     if (validateSurvey){
      return true;
      }
     else
     { 
     alert("Please Answer All Questions!");
     return false;
     }
    });

    });


   function Checkform() {
    var result = true;
    var categories= $('.radioList').find('.controlsetRadio');
    $(categories).each(function() {
    var checked = $(this).find('input:radio:checked');
    var count = 0;
    if (checked.length !=1) {
            result = false;
              $(this).addClass('error');       
       }else{
         $(this).removeClass('error');
       }
    });


    return result;
}

</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top