0

我有许多带有不同按钮的行。当我单击一个按钮时,它会检查它是否是该行中的正确答案。这个我已经在工作了。我想不通的是如何给那个正确的按钮(或错误的)一个不同的颜色,所以它表明用户是正确的(或错误的)。有任何想法吗?

这是在我的控制器中:

//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 的东西。看起来我应该能够使用它,但我不知道如何开始。有谁会?

4

1 回答 1

0

您最好的选择是为特定按钮添加一个类(即:“正确”或“不正确”),然后将相关的 CSS 添加到您的样式表中:

.correct{
    colour: #11eb00;
}
.incorrect{
    colour: #eb0000;
}
于 2013-05-23T12:11:49.893 回答