简体   繁体   中英

Checking Against Specific Checkboxes with the Answers

I have already done a lot of research on Stack Overflow to attempt to answer this question, but most answers are to specific to answer this.

I am attempting to lock questions after the answers are selected and tell the user if the question is right or wrong.

My solution for a Multiple Checkbox style question was to have a 'confirm' button after they select there answers. In this case, I have built a generic function for me to pass the answers to and have them mapped for me. However, I am struggling to come up with a solution for Multiple Checkboxes.

The solution must revolve around javascript/jquery as I do not have control over the HTML generated from this Form Builder that I use.

TL;DR

My solution below fails to correctly recognize wrong answers for Question 14, it should only pop up 'Correct!' if option 3 AND 4 are both selected.

Currently, it pops up correct if they select 3 and any other option.

For a version to work with: 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

I was able to come up with the answer this morning.

My problem was on this for loop, I'm looping through the answers, which I passed 23, if I chose 2 and 4 (or in the fiddle, 1 and 3). Then it responded as correct, because on the last iteration it would find that the last checkbox was check and 2 checkboxes were checked.

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

My Solution was to Iterate through which checkboxes are checked and return the combined index of which was checked and compare that to the answers.

EG I pass '23', they check '1' and '4', '14' != '23', so incorrect.

$("#"+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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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