for ($i=0; $i<$total_questions_for_exam; $i++) {
$question_id= $question_and_answers[$i]['question_id'];
$numberofanswersperquestion = count_answers_belongToOne_questionNew($question_id);
//die(var_dump($question_id));
$student_answer_per_question = retrieve_student_result ($_SESSION['user_id'], $_GET['quiz_id'], $question_id);
$correct_answer = $numberofanswersperquestion[0]['answer_name'];
echo ' <form method="post" id="review_form" name="review_form" action="view_user_summary.php">
<table class="table table-bordered table-condensed table-datatable table-hover review_marks">
<tbody>
<tr>
<td style="text-align: left;" width="100%"><strong>Question '. ($i+1) .'</strong></td>
</tr>
<tr>
<td style="text-align: left;" width="100%">' . $question_and_answers[$i]['question_name'] .'</td>
</tr>';
if($student_answer_per_question == '')
echo '<tr>
<td style="text-align: left;" width="100%" class="warning"><em>Question Not attempted</em><br><strong>Correct Answer is </strong><br>' . $numberofanswersperquestion[0]['answer_name'] . '</td>
</tr>';
else if ($student_answer_per_question == $correct_answer)
echo '<tr>
<td style="text-align: left;" width="100%" class="success"><strong>Your answer is correct.</strong><br>' . $student_answer_per_question .'</td>
</tr>';
else
echo '<tr>
<td style="text-align: left;" width="100%" class="danger">
<table style="width: 100%">
<tbody>
<tr>
<th style="width: 50%"><strong>Your Answer</strong></th>
<th style="width: 50%"><strong>Correct Answer</strong></th>
</tr>
<tr>
<td style="width: 50%">' . $student_answer_per_question . '</td>
<td style="width: 50%">' . $correct_answer .'</td>
</tr>
</tbody>
</table>
</td>
</tr>';
echo
'<tr>
<td style="height: 5px;" width="100%"> </td>
</tr>
<tr>
<td style="height: 5px;" width="100%"> </td>
</tr>
</tbody>
</table>
<div class="[ form-group ] correct_answer">
<input type="checkbox" name="fancy-checkbox-primary" id="fancy-checkbox-primary" autocomplete="off" />
<div class="[ btn-group ]">
<label for="fancy-checkbox-primary" class="[ btn btn-primary ]">
<span class="[ glyphicon glyphicon-ok ]"></span>
<span> </span>
</label>
<label for="fancy-checkbox-primary" class="[ btn btn-default active ]">
Correct Answer
</label>
</div>
</div>';
}
echo '<button class="btn btn-large btn-block btn-primary submitbutton" type="submit" style="margin-top:100px;" name="submit_review">Submit Review</button></form>';
嗨,伙计们,上面的这段代码基本上只是提取我数据库中的所有问题和问题。我试图实现的是在这个for循环中有一个复选框,它允许用户勾选该框以表示答案是正确的,或者如果他们不做任何事情,则假设答案是错误的。
外观截图
它显示为我想要的样子,但有一个问题,当我单击第二个复选框时,第一个复选框仍在滴答作响,我无法控制第二个复选框。知道我该如何解决这个问题吗?
认为展示我的意思的视频是个好主意,我提供了一个链接。
问题
你有一个循环:
<input type="checkbox" name="fancy-checkbox-primary" id="fancy-checkbox-primary" autocomplete="off" />
这意味着您的复选框都具有相同的id
。 这是无效标记,并且尝试使用该id
的任何代码的行为都将是未定义的。 (它可能会找到第一个匹配元素、最后一个匹配元素、所有匹配元素、没有匹配元素等(
顾名思义,id
意在标识一个元素。 您需要更正标记,以便可以正确识别元素,并调整使用这些元素来标识它们的任何代码。
这可能涉及将值附加到循环中的id
,使每个值都唯一。 或者也许为此使用 class
而不是id
。 从问题中的代码来看,我无法真正与后者交谈,但前者应该足够简单:
<input type="checkbox" name="fancy-checkbox-primary" id="fancy-checkbox-primary-' . $i . '" autocomplete="off" />
(注意:我不完全熟悉您用来设置这些复选框样式的机制,显然这里有一些插件在起作用。 您可能还需要类似地修改与此id
值关联的name
和/或for
属性。 基本上代码中的任何内容,显示在问题或其他方面,都依赖于id
。
虽然仍然不是 100% 清楚这对问题中未包含的代码的影响是什么,所以你可能需要修补你自己的需求。 但从根本上说,这里的问题是重复的id
价值观。