简体   繁体   English

jQuery拖放测验-具有多个拖放答案

[英]jQuery Drag and Drop Quiz - Having multiple drag and drop answers

I currently have a drag and drop interface that allows an answer to be dragged into a 'destinationBox' and it will mark the answer correct or incorrect. 我目前有一个拖放界面,该界面允许将答案拖放到“ destinationBox”中,并将答案标记为正确或不正确。 It does this by matching the questions ID to the answers Class - see code below 它通过将问题ID与答案Class匹配来实现-参见下面的代码

<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);
    });
}

The problem is, I need to have more than one answer per question. 问题是,每个问题我需要多个答案。 Currently if I give two answers the same class of a1 it only shows the first one as being correct. 目前,如果我给两个答案是相同的a1类,则只会显示第一个正确。 From my understanding this is because my code is looking through the HTML to find the matching class and once it has found one it stops and doesn't carry on looking for any other matching classes. 从我的理解来看,这是因为我的代码正在通过HTML查找匹配的类,一旦找到匹配的类,它将停止并且不会继续寻找任何其他匹配的类。 I'm quite new to JavaScript/jQuery and am at a bit of a loss on where to go now. 我是JavaScript / jQuery的新手,现在不知所措。 Any help is greatly appreciated! 任何帮助是极大的赞赏!

codepen.io/anon/pen/GpYPRK codepen.io/anon/pen/GpYPRK

Not sure if this would work, as already mentioned a fiddle would help. 如前所述,不确定是否会奏效。 Try changing this line 尝试更改此行

 var answer = $('.' + number).html();

To this 对此

var answer = $(index).find('.' + number + ':first()').html();

If I have understood correctly then all the answers to your question 'q1' would be in different div tags with class a1. 如果我正确理解,那么对您的问题“ q1”的所有答案将位于类a1的不同div标签中。 So in that case what you can do is first find all the a1's. 因此,在这种情况下,您可以首先找到所有a1。 Then for each a1 find the innerHtml and store it in an array. 然后为每个a1查找innerHtml并将其存储在数组中。 So you will have all the answers in an array. 因此,您将在阵列中得到所有答案。

var answers = $('.' + number); 
answers.each(function(){
  var answer = $(this).html();
  //now you can store this answer in an array say "arr"
  arr.push(answer);
  //Rest of your logic
});

I have not tested this code so there may be syntax errors but the logic should give you some idea. 我没有测试此代码,因此可能存在语法错误,但逻辑应能使您有所了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM