简体   繁体   中英

Javascript not checking whether a checkbox was selected or not

I'm trying to build a quiz with multiple choice questions, one of which has multiple correct answers. So I'm trying to check which checkboxes in my questions have been selected by a student in order to give the right feedback. my code is:

for(var i = 0; i< input1.length; i++)
    {
        if(input1[0].checked && input1[1].checked)
        {
        submit_answer.onclick = showFeedback1; 
        }
        else
        {
        submit_answer.onclick = false1; 
        }
    }

It never takes the first if, even if I select those two only. No matter what I put in the if statement, it only takes the else.

and this is just a part of my .js

var quiz = document.getElementById('quiz');

var questions = quiz.getElementsByTagName('p');

input1 = questions[0].getElementsByTagName('input');

var submit_answer = document.getElementById('submit_answers'); // this is the submit button

I cannot make proper assumption of what you are trying to do.

FIRST PROBLEM

Your for loop is incrementing on 1, so on the each next iteration it is comparing using same previously used value.

SECOND PROBLEM

Your structure is horrible, your script fetches up all the input elements inside every p. You should properly organize your element in groups and then match whether or not they are checked.

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