当我点击正确答案时,如何改变按钮的颜色?它在PHP中进行了测试.(CodeIgniter)

How can I change the color of a button when I clicked the right answer? It is tested in PHP. (CodeIgniter)

本文关键字:按钮 颜色 它在 测试 CodeIgniter PHP 改变 答案 何改变      更新时间:2023-09-26

我有许多带有不同按钮的行。当我点击一个按钮时,它会检查那一行的答案是否正确。这个我已经做过了。我不知道的是如何给正确的按钮(或错误的)一个不同的颜色,所以它显示用户是正确的(或错误的)。什么好主意吗?

这是在我的控制器:

//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="answerswer" value="' . $instruction . ',' . $label .'">' . $instruction . '</button>';
                            }
                            else
                            {
                                $dump .= '<button type="submit" name="answerswer" value="' . $instruction . ',' . $label .'">' . $instruction . '</button>';    
                            }
                        }
                        $dump .= '</li>';
                    }
                    $dump .= '</ul>';
                    echo $dump;
                ?>
编辑:

我一直在研究jQuery事件。目标的东西。看起来我应该可以用它,但我不知道怎么开始。有人知道吗?

最好的办法是为特定的按钮添加一个类(即:'correct'或'incorrect'),然后将相关的CSS添加到样式表中:

.correct{
    colour: #11eb00;
}
.incorrect{
    colour: #eb0000;
}