Yeah, like I thought, I'm back again.  ;-)

Below is the code I've been cobbling together from your previous post.
Please feel free to slap me around and tell me where I've done bad.
Right now, the form validation works fine (if there is nothing
selected then the jquery validation comes up). However, when I hit the
submit button, nothing happens. I'm sure I've screwed something up
royally but I'm not an experienced enough programmer to figure it all
out before the sun dies.

<script type="text/javascript">
$.validator.setDefaults({
submitHandler: function() {
var correctAnswer = 1
$('.theAnswer').each(
   function(){
   var temp_val =  $('.theAnswer').val();
   var temp_id =  $('.theAnswer').attrib('id');
   if(temp_val=='selected'){
       if(temp_id == correctanswer){
          alert("Correct!");
       }
       else{
          alert("Try Again.");
      }
   }
});
}
});

$.metadata.setType("attr", "validate");
$(document).ready(function() {
$("#quiz_form").validate();
});

</script>

</head>
<body>
<div id="main">
<form class="cmxform" id="quiz_form" method="get" action="">
        <fieldset>
                <fieldset>
                        <label for="answer1">
                <input name="answers"  type="radio" class="theAnswer"
id="answer1" value="1" validate="required:true" />
                                Answer 1
                        </label>
                        <label for="answer2">
                                <input name="answers"  type="radio" 
class="theAnswer" id="answer2"
value="2" />
                                Answer 2
                        </label>
                        <label for="answer3">
                                <input name="answers"  type="radio" 
class="theAnswer" id="answer3"
value="3" />
                                Answer 3
                        </label>
                        <label for="answer4">
                                <input name="answers"  type="radio" 
class="theAnswer" id="answer4"
value="4" />
                                Answer 4
                        </label>
                        <label for="answers" class="error">Please select an 
answer.</
label>
                </fieldset>
                <input class="submit" type="submit" value="Check Answers" />
        </fieldset>
</form>
</div>
</body>
</html>



On Jun 25, 4:27 pm, Matt Riley <mattrileyiph...@gmail.com> wrote:
> Thanks for your reply. I'm going to give it a go with what you
> suggested, although I'm not exactly a seasoned pro at this so I will
> probably get stuck and come back begging for more help.  :-)
>
> I should have mentioned that this thing needs to run locally from a
> user's hard disk without a requirement for server-side processing. If
> this was not the case, I would've probably pursued a php solution as
> I'm slightly more comfortable with that (jquery is very new to me). It
> doesn't much matter if the correct answer is hidden in the source of
> the page somewhere; these quizzes will just be used for basic users
> who wouldn't know/care to look in the source anyway.
>
> Again, thank you for your reply. I'll probably be back. LOL.
>
> -Matt
>
> On Jun 25, 3:16 pm, Lee R Lemon III <leerlemon...@gmail.com> wrote:
>
>
>
> > well like many things this has a variable answer...
>
> > but what I think you will need to do is write a script that gets the
> > correct answer (I would use ajax calls to server so the answer can not
> > be seen in the browser code)
> > then set an event on the submit button that cycles through your radio
> > buttons for the correct answer easiest way to do that is add a class
> > to each and do something sorta like..
> > //outside a function (before your document.ready as well)
> > var correctanswer;
> > //somewhere set the value of correctanwser
> > $('.your_class').each(
> >    function(){
> >    var temp_val =  $('.your_class').val();
> >    var temp_id =  $('.your_class').attrib('id');
> >    if(temp_val=='selected'){
> >        if(temp_id == correctanswer){
> >            //code for correct answer here
> >        }
> >        else{
> >           //code for wrong answer here
> >       }
> >    }
>
> > )
>
> > On Jun 25, 2:35 pm, Matt Riley <mattrileyiph...@gmail.com> wrote:
>
> > > I'm making a simple quiz using jquery and the validate plug-in. I
> > > think I'm really close but just need a nudge over the last hump.  :-)
>
> > > I have a radio button group that requires the user to select at least
> > > one button. That part is working. However, what I want to do is also
> > > check to see if the user selected a certain button (i.e. the correct
> > > answer). Only if the correct answer is satisfied will the form then
> > > validate and move on to the next quiz question.
>
> > > Here's my code so far. Any help is greatly appreciated.
>
> > > <script type="text/javascript">
> > > $.validator.setDefaults({
> > > submitHandler: function() {
> > > alert("Submitted!");
>
> > > }
> > > });
>
> > > $.metadata.setType("attr", "validate");
>
> > > $(document).ready(function() {
> > >         $("#quiz_form").validate();});
>
> > > </script>
>
> > > </head>
> > > <body>
>
> > > <div id="main">
>
> > > <form class="cmxform" id="quiz_form" method="get" action="">
> > >         <fieldset>
> > >                 <fieldset>
> > >                         <label for="answer1">
> > >                                 <input  type="radio" id="answer1" 
> > > value="1" name="answers"
> > > validate="required:true" />
> > >                                 Answer 1
> > >                         </label>
> > >                         <label for="answer2">
> > >                                 <input  type="radio" id="answer2" 
> > > value="2" name="answers" />
> > >                                 Answer 2
> > >                         </label>
> > >                         <label for="answer3">
> > >                                 <input  type="radio" id="answer3" 
> > > value="3" name="answers" />
> > >                                 Answer 3
> > >                         </label>
> > >                         <label for="answer4">
> > >                                 <input  type="radio" id="answer4" 
> > > value="4" name="answers" />
> > >                                 Answer 4
> > >                         </label>
> > >                         <label for="answers" class="error">Please select 
> > > an answer.</label>
> > >                 </fieldset>
> > >                         <input class="submit" type="submit" value="Check 
> > > Answers"/>
> > >         </fieldset>
> > > </form>
> > > </div>
>
> > > </body>
> > > </html>

Reply via email to