【发布时间】:2015-11-09 09:37:22
【问题描述】:
我目前有一个拖放界面,允许将答案拖到“目标框”中,它会标记答案正确或不正确。它通过将问题 ID 与答案类匹配来做到这一点 - 请参见下面的代码
<div class="question textbox" id="q1">
1. Assemble the Crisis Management Team
</div>
<div class="destinationBox"></div>
<td>
<div class="dragDropSmallBox answer a1">0123456789</div>
</td>
// initialisation for all things regarding questions.
function init_questions() {
questionList = Array();
$(".question").each(function(index) {
// find questions and assign answers to them
var question = new Question(this);
question.body = this.innerHTML;
//get id of question
var id = $(this).attr('id');
//replace 'q' in ID with 'a' instead
var number = id.replace(/q/g, 'a');
var answer = $('.' + number).html();
question.answers.push(answer);
questionList.push(question);
});
}
问题是,每个问题我需要有多个答案。目前,如果我在a1 的同一类中给出两个答案,它只会显示第一个是正确的。据我了解,这是因为我的代码正在浏览 HTML 以查找匹配的类,一旦找到匹配的类,它就会停止并且不会继续寻找任何其他匹配的类。我对 JavaScript/jQuery 很陌生,现在不知道该去哪里。任何帮助是极大的赞赏!
codepen.io/anon/pen/GpYPRK
【问题讨论】:
-
您能给我们提供一个 JSFiddle/Codepen 吗?
-
抱歉,这是一个codepen :) codepen.io/anon/pen/GpYPRK
标签: javascript jquery html