使用答案检查特定复选框

Checking Against Specific Checkboxes with the Answers

我已经对 Stack Overflow 进行了大量研究以尝试回答这个问题,但大多数答案都是针对这个问题的。

我试图在回答 select 后锁定问题,并告诉用户问题是对还是错。

我对多复选框样式问题的解决方案是在 select 答案后有一个 'confirm' 按钮。在这种情况下,我已经为我构建了一个通用函数来将答案传递给并为我映射它们。但是,我正在努力为多个复选框提出解决方案。

解决方案必须围绕 javascript/jquery,因为我无法控制从我使用的这个 Form Builder 生成的 HTML。

TL;DR

我下面的解决方案无法正确识别问题 14 的错误答案,它应该只在选项 3 和 4 都被 selected 时弹出 'Correct!'。

目前,如果 select 3 和任何其他选项,它会正确弹出。

要使用的版本:Fiddle

HTML:

<div id="q14" class="q required highlight">
<a class="item_anchor" name="ItemAnchor25"></a>
<span class="question top_question">14. Life policy purchase evaluations include which of the following (choose all that apply):&nbsp;<b class="icon_required" style="color:#FF0000">*</b></span>
<table class="inline_grid">
<tbody><tr>
<td><input type="checkbox" name="RESULT_CheckBox-25" class="multiple_choice" id="RESULT_CheckBox-25_0" value="CheckBox-0" readonly=""><label for="RESULT_CheckBox-25_0">Insured’s credit rating</label></td>
</tr>
<tr>
<td><input type="checkbox" name="RESULT_CheckBox-25" class="multiple_choice" id="RESULT_CheckBox-25_1" value="CheckBox-1" readonly=""><label for="RESULT_CheckBox-25_1">Employment history</label></td>
</tr>
<tr>
<td><input type="checkbox" name="RESULT_CheckBox-25" class="multiple_choice" id="RESULT_CheckBox-25_2" value="CheckBox-2" readonly=""><label for="RESULT_CheckBox-25_2">Medical records</label></td>
</tr>
<tr>
<td><input type="checkbox" name="RESULT_CheckBox-25" class="multiple_choice" id="RESULT_CheckBox-25_3" value="CheckBox-3" readonly=""><label for="RESULT_CheckBox-25_3">Insurer’s credit rating</label></td>
</tr>
</tbody></table>
</div>

Javascript:

$(document).ready(function(){
var content = document.body.textContent || document.body.innerText;
var hasText = content.indexOf("10. What is the minimum initial investment for a non-retirement investor in this program?")!==-1;

//page 4 questions
if(hasText){
    var correctNumber = [1, 0, 0, 1, 23, 1];
    createQuiz(correctNumber);
}

//Dynamically grabs questions, answers, and creates alert boxes for each section of radio buttons
function createQuiz(correctNumber){

    $("span.question").each(function(i){

        var question = $(this).parent("div").find($(".multiple_choice")).attr("name"); //get the question name attribute
        var parentDiv = $(this).parent("div").attr("id"); //get the parent div id

        //set the alert box HTML
        $(this).parent("div").after("<br><br><br><div id='"+parentDiv+"_success' class='alert alert-success'><strong>Correct!</strong></div><div id='"+parentDiv+"_fail' class='alert alert-danger'><strong>Incorrect!</strong></div>");
        $("#"+parentDiv+"_fail").hide(); //hide alert box
        $("#"+parentDiv+"_success").hide();//hide alert box

        if($(this).parent("div").find($(".multiple_choice")).attr("type")=='radio'){

            $("input[name='"+question+"']").on("click keypress", function(){

                $("input[name='"+question+"']").prop('disabled', true);
                $("input[name='"+question+"']:radio:not(:checked)").attr('disabled', true);
                $(this).attr('disabled', false);

                if($("#"+question+"_"+correctNumber[i]).is(':checked')){
                    $("#"+parentDiv+"_success").show();
                    $("#"+parentDiv+"_fail").hide();
                } 
                else {
                    $("#"+parentDiv+"_success").hide();
                    $("#"+parentDiv+"_fail").show();
                }

                $('.tooltip').tooltipster({
                    contentAsHTML: true,
                    maxWidth: '480',
                    animation: 'grow',
                    side: ['right', 'top', 'bottom', 'left']
                });

            }); //end on click/keypress

        } else if ($(this).parent("div").find($(".multiple_choice")).attr("type")=='checkbox'){

            $("#"+parentDiv+"_success").before("<button id='"+parentDiv+"_confirm' style='width:200px; left:16px; position:relative;' type='button' value='Confirm'>Confirm</button>");

            $("#"+parentDiv+"_confirm").on("click keypress", function(){

                $("input[name='"+question+"']").prop('readonly', true);

                var answers = correctNumber[i].toString().split("");

                for(p=0; p < answers.length; p++){

                    var selected = [];
                    $("input[name='"+question+"']").filter(":checked").each(function() {

                        if(question+"_"+answers[p] == $(this).attr('id') && $("input[name='"+question+"']").filter(":checked").length == answers.length){
                            $("#"+parentDiv+"_success").show();
                            $("#"+parentDiv+"_fail").hide();
                        } 
                        else {
                            $("#"+parentDiv+"_success").hide();
                            $("#"+parentDiv+"_fail").show();
                        }

                    });

                    // if($("#"+question+"_"+answers[p]).is(':checked') && $("input[name='"+question+"']").filter(":checked").length == answers.length){
                    //  $("#"+parentDiv+"_success").show();
                    //  $("#"+parentDiv+"_fail").hide();
                    // } 
                    // else {
                    //  $("#"+parentDiv+"_success").hide();
                    //  $("#"+parentDiv+"_fail").show();
                    // }

                    $('.tooltip').tooltipster({
                        contentAsHTML: true,
                        maxWidth: '480',
                        animation: 'grow',
                        side: ['right', 'top', 'bottom', 'left']
                    });

                } //end for loop

            }); //end on click/keypress

        } //end outer if

    }); //end outer each
} //end createQuiz function



}); //end document ready

今天早上我想出了答案。

我的问题出在这个 for 循环上,如果我选择 2 和 4(或在 fiddle、1 和 3 中),我正在循环通过答案,我通过了 23。然后它响应正确,因为在最后一次迭代中它会发现最后一个复选框被选中并且选中了 2 个复选框。

for(p=0; p < answers.length; p++){

     var selected = [];
     $("input[name='"+question+"']").filter(":checked").each(function() {

        if(question+"_"+answers[p] == $(this).attr('id') && $("input[name='"+question+"']").filter(":checked").length == answers.length){
           $("#"+parentDiv+"_success").show();
           $("#"+parentDiv+"_fail").hide();
        } 
        else {
           $("#"+parentDiv+"_success").hide();
           $("#"+parentDiv+"_fail").show();
        }

     });

} //end for loop

我的解决方案是遍历哪些复选框被选中,return 其组合索引被选中,并将其与答案进行比较。

我传递 '23',他们检查 '1' 和 '4','14' != '23',所以不正确。

$("#"+parentDiv+"_confirm").on("click keypress", function(){

  $("input[name='"+question+"']").prop('readonly', true);

  var answers = correctNumber[i];

  var input = "";

  $("input[name='"+question+"']").each(function(t){

      if($(this).is(':checked')){
          input = ""+input+t;
      }

  });

  if(input == answers){
      $("#"+parentDiv+"_success").show();
      $("#"+parentDiv+"_fail").hide();
  } 
  else {
      $("#"+parentDiv+"_success").hide();
      $("#"+parentDiv+"_fail").show();
  }


}); //end on click/keypress