我有许多行具有不同的按钮。当我单击一个按钮时,它会检查该行中的答案是否正确。我已经在工作了。我无法弄清楚的是如何给出正确的按钮(或错误的)不同的颜色,因此它表明用户是正确的(或错误的)。 有什么想法吗?
这是在我的控制器中:
//function for press exercises: exercises where you make decissions between buttons
public function press($ped_mat_id)
{
if($this->input->post('answer'))
{
$answer = $this->input->post('answer');
$evaluation = $this->Exercise->check_exercise_row($answer);
if($evaluation)
{
//Code when the answer is correct
echo 'correct';
echo '<style type="text/css">
button {
background-color: #11eb00 !important;
}
</style>';
}
else
{
//Code when the answer is wrong
echo 'fout';
echo '<style type="text/css">
button {
background-color: #eb0000 !important;
}
</style>';
}
}
$this->_load_views('Oefen', 'exercises/press.php', $ped_mat_id);
}
这是在我的模型中:
public function check_exercise_row($answer)
{
$arr_answer = explode(',', $answer);
$given_answer = $arr_answer[0];
$exercise_id = $arr_answer[1];
$result = $this->_get_solution($exercise_id);
foreach ($result as $row)
{
$instruction = $row->exercise_instruction;
$solution = $row->exercise_solution;
}
$arr_instruction = explode(',',$instruction);
$is_correct = false;
if($given_answer == $arr_instruction[$solution])
{
$is_correct = true;
$this->_insert_answer($given_answer, $exercise_id,$is_correct);
return $is_correct;
}
else
{
$this->_insert_answer($given_answer, $exercise_id,$is_correct);
return $is_correct;
}
}
这在我看来:
<?php
$dump = '<ul>';
foreach ($result as $row)
{
$label = $row->exercise_id;
$arr_instructions = explode(',', $row->exercise_instruction);
$dump .= '<li><label>' . $label . '</label>';
foreach ($arr_instructions as $instruction)
{
if (in_array($label, $log) )
{
$dump .= '<button type="submit" disabled="disabled" name="answer" value="' . $instruction . ',' . $label .'">' . $instruction . '</button>';
}
else
{
$dump .= '<button type="submit" name="answer" value="' . $instruction . ',' . $label .'">' . $instruction . '</button>';
}
}
$dump .= '</li>';
}
$dump .= '</ul>';
echo $dump;
?>
修改 我一直在研究jQuery event.target的东西。它看起来我应该能够使用它,但我不知道如何开始。谁做了?
答案 0 :(得分:0)
您最好的选择是在特定按钮上添加一个类(即:'correct'或'wrong'),然后将相关的CSS添加到样式表中:
.correct{
colour: #11eb00;
}
.incorrect{
colour: #eb0000;
}