Hi Matt
Sorry, I omitted to change the variable you are comparing the value of. Ie
return this.optional(element) || value > params;
should read >
return this.optional(element) || radioValue > params;
Also stick a breakpoint where the variable is created and just make sure it is actually populated. Should be - but be sure!! Hope that helps Matt!!
Regards,
Alletha
-----Original Message-----
From:
jquery-en@googlegroups.com [mailto:jquery-en@googlegroups.com] On Behalf Of Matt Riley
Sent: Tuesday, 30 June 2009 2:27 AM
To: jQuery (English)
Subject: [jQuery] Re: (validate) Radio button values
Thanks guys. I really appreciate all the help.
I'm still stuck, though. I can't get anything to work now, although I
can't seem to figure out why. I'm definitely not a programmer, so
please be gentle, though feel free to explain things to me like I'm a
5 year-old. I won't be offended. :-)
Here's where I'm at after trying to integrate your posts. Again, this
code doesn't produce any results, so I'm really stuck.
<script type="text/javascript">
jQuery.validator.addMethod("correctAnswer", function(value, element,
params) {
var radioValue = $("#main").find("input[type='radio']").val(); //
returns the selected value from the radio button group
return this.optional(element) || value > params;
}, "Select the correct answer to move on.");
$.validator.setDefaults({
submitHandler: function() {
alert("Go to next question.");
}
});
$.metadata.setType("attr", "validate");
$(document).ready(function() {
$("quiz_form").validate({
rules: {
correctAnswer: 1
}
});
});
</script>
</head>
<body>
<div id="main">
<form id="quiz_form" method="get" action="">
<fieldset>
<fieldset>
<label for="answer1">
<input class="{correctAnswer:'1'}" type="radio" id="answer1"
value="1" name="answers" />
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>
All I want to be able to do is present the user with four answers and
force the user to pick the correct answer before being allowed to move
to the next question. It doesn't matter if the answer is in the code
as the target for this is NOT the type who would open up the source
and go through it. :-)
-Matt