简体   繁体   English

Javascript没有检查是否选中了复选框

[英]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. 无论我在if语句中添加什么内容,它只需要其他内容。

and this is just a part of my .js 这只是我的.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. 你的for循环在1上递增,因此在每次下一次迭代时,它使用相同的先前使用的值进行比较。

SECOND PROBLEM 第二个问题

Your structure is horrible, your script fetches up all the input elements inside every p. 你的结构太可怕了,你的脚本会抓取每个p里面的所有输入元素。 You should properly organize your element in groups and then match whether or not they are checked. 您应该在组中正确组织元素,然后匹配它们是否被选中。

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

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