php - Need some help for radio button change function -
http://keralapsctuts.com/old/topic.php?id=14
the above page working on have problem. after loading questions , choices mysql database, have feature whereby user selects choice, checks see if answer correct or not, if selection correct text changes green or red (if incorrect selection).
this layout of typical question/choice:
'<div class="question_container"> <p class="questions">'.$question.'</p> <span class="group"> <input id="id_radio1" type="radio" name="group'.$id.'" value="'.$ans1.'" />'.$ans1.' <input id="id_radio2" type="radio" name="group'.$id.'" value="'.$ans2.'" />'.$ans2.' <input id="id_radio3" type="radio" name="group'.$id.'" value="'.$ans3.'" />'.$ans3.' <input id="id_radio4" type="radio" name="group'.$id.'" value="'.$ans4.'" />'.$ans4.' </span> </div>';
you don't give enough info, may similar looking for:
question/answer block:
<div class="question_container"> <p class="questions">question stuff?</p> <span class="group" style="inline-block;"> <label> <input id="id_radio1" class="incorrect" type="radio" name="group1" value="answer 1" />answer 1 </label> <label> <input id="id_radio2" class="correct" type="radio" name="group1" value="answer 2" />answer 2 </label> <label> <input id="id_radio3" class="incorrect" type="radio" name="group1" value="answer 3" />answer 3 </label> <label> <input id="id_radio4" class="incorrect" type="radio" name="group1" value="answer 4" />answer 4 </label> </span> </div>
jquery:
$("input[type=radio]").click(function() { var radio = $(this); var getgroup = radio.parents(".group"); getgroup.find("label").css({"background-color":"transparent"}); if (radio.attr("class") == 'correct') radio.parent().css({"background-color":"green"}); else radio.parent().css({"background-color":"red"}); });
Comments
Post a Comment