0

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> 
Mbayader
  • 13
  • 1
  • 3
  • 1
    Please put down corresponding HTML as well. – Bagira Apr 02 '12 at 00:05
  • @Mbayader whats the problem you are facing? Remember S2 tags in end will be same as HTML tags so what ever applicable to HTML tags is equally applicable to S2 tags – Umesh Awasthi Apr 02 '12 at 02:13
  • Someone just asked a question for which I just supplied an answer which applied to the server side validation which you should implement (the client can send anything): http://stackoverflow.com/questions/9969024/struts2-validation-for-an-array with that done... show the resulting html to the jQuery people and remove all reference to struts2, because at that point it is just a regular web page. – Quaternion Apr 02 '12 at 02:18

2 Answers2

0

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;
}
gooogenot
  • 499
  • 4
  • 15
0

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>
Mbayader
  • 13
  • 1
  • 3