当我点击正确答案时,如何改变按钮的颜色?它在PHP中进行了测试.(CodeIgniter)
How can I change the color of a button when I clicked the right answer? It is tested in PHP. (CodeIgniter)
我有许多带有不同按钮的行。当我点击一个按钮时,它会检查那一行的答案是否正确。这个我已经做过了。我不知道的是如何给正确的按钮(或错误的)一个不同的颜色,所以它显示用户是正确的(或错误的)。什么好主意吗?
这是在我的控制器:
//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;
}
相关文章:
- 使用event.target.classlist区分按钮颜色
- 如何使用带有多个按钮的javascript更改按钮颜色
- Highcharts上下文按钮颜色
- 在提交时更改按钮颜色,直到输入新文本
- 如何在javascript中更改按钮颜色
- 根据字段值更改页面加载时的按钮颜色
- 我想在单击时更改按钮颜色和文本,并且在刷新页面时它不应该更改
- 单击另一个按钮后如何更改按钮颜色
- 按钮颜色在切换时不会更改多个按钮
- 默认按钮颜色
- 当我使用jQuery Mobile的UI时,如何在jQuery Mobile中切换单击的导航按钮颜色
- 当管理员在管理面板中获得新任务时,如何更改菜单按钮颜色
- 单击时更改按钮颜色
- 无法更改Bootbox中确认框的按钮颜色
- 更改按钮颜色,即使在单击后也是如此
- 根据表中的另一列更改按钮颜色
- 使用addEventListener切换按钮颜色
- AJAX数据库更新与CSS按钮颜色改变使用Codeigniter
- 单选按钮颜色选择器
- 随着背景颜色的变化而改变按钮颜色