Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查用户是否标记了js答案_Javascript - Fatal编程技术网

Javascript 检查用户是否标记了js答案

Javascript 检查用户是否标记了js答案,javascript,Javascript,我正在写一个带问题的简单测验,我对如何编写代码感到进退两难。也就是说,我想写一个函数来检查用户是否标记了给定的答案 因此,我假设用户知道问题的答案,并标记该答案。因此,我给自己设置了一个名为const isChecked的变量,它为我存储了一个布尔true值,因为我假设用户选择了给定问题的答案 但是,如果出于某种原因(例如,它不知道问题的答案)而不选择问题,则此变量现在存储逻辑值false,并且当用户尝试单击发送按钮时,负责显示后续问题的函数将不会运行 我有这样一个概念。我创建了一个使我保持为真

我正在写一个带问题的简单测验,我对如何编写代码感到进退两难。也就是说,我想写一个函数来检查用户是否标记了给定的答案

因此,我假设用户知道问题的答案,并标记该答案。因此,我给自己设置了一个名为
const isChecked
的变量,它为我存储了一个布尔
true
值,因为我假设用户选择了给定问题的答案

但是,如果出于某种原因(例如,它不知道问题的答案)而不选择问题,则此变量现在存储逻辑值
false
,并且当用户尝试单击发送按钮时,负责显示后续问题的函数将不会运行

我有这样一个概念。我创建了一个使我保持为真的变量。将此变量命名为
isChecked

const isChecked = true;
因为正如我所说,我假设用户知道给定问题的答案,并希望继续前进。因此,符合条件。所以如果

if (isChecked === true) {
  // the code will be executed because the user marked the answer
}
但是,如果用户不知道问题的答案,则变量isChecked将保存值
false
const isChecked=false
。因此,如果执行失败,将执行
false
,但将执行else:

if (isChecked === true) {
  // the code will not execute because the user has not selected the answer
} else {
  // the code will execute because the value is true. And at this point I want to block the user from going to the next question when trying to press the send button.
}

阿皮卡卡·奎祖
提交
这是我迄今为止的全部代码:

让oldQuestionIndex=-1;
const headerQuestions=document.getElementById('header-questions');
const answerA=document.getElementById('a');
const answerB=document.getElementById('b');
const answerC=document.getElementById('c');
const answerD=document.getElementById('d');
让arrAnswerVaranty=['a','b','c','d'];
const btnSubmit=document.querySelector('.btn submit');
康斯特奎扎尔=[
{
问题:“2019年最常用的编程语言是什么?”,
答案:['Java','C','Python','JS']
},
{
问题:“谁是美国总统?”,
答案:[‘弗洛林流行音乐’、‘乔·拜登’、‘伊万·萨尔达诺’、‘米海·安德烈’]
},
{
问题:“HTML代表什么?”,
答复:[
“超文本标记语言”,
“级联样式表”,
“Jason对象表示法”,
“直升机码头摩托艇兰博基尼”
]
},
{
问题:“JavaScript发布于哪一年?”,
答案:[‘1996’、‘1995’、‘1994’、‘以上均无’]
}
];
功能演示回答(回答){
for(quizArr中的const id){
document.getElementById(arrAnswerVaranty[id]).innerHTML=answers[id];
}
}
函数showRandomQuestions(){
做{
var randomQuestinex=Math.floor(Math.random()*quizArr.length);
}while(randomQuestIndex==oldQuestionIndex);
oldQuestionIndex=randomQuestIndex;
headerQuestions.innerHTML=quizArr[randomQuestIndex]。问题;
showAnswersQuiz(quizArr[RandomQuestinex]。答案);
}
函数isValid(){
for(设i=0;i
它为上述问题编写的函数称为
checkIfChecked
。我只是想知道这是正确的概念还是你有更好的概念

  • 常量变量在初始化后不能更改其值
  • 若您使用复选框让用户检查答案,则函数如下
  • 功能已检查(复选框){
    如果(checkbox.checked==true){
    返回true;
    }
    否则{
    返回false;
    }
    }
    已检查(document.getElementsByClassName('answer1')[0])//调用示例(将其用作bool变量的值)

    在单击单选按钮之前,“提交”按钮处于禁用状态,当单击“提交”时,将取消检查所有收音机中的下一个问题。为了更好地理解,我在代码中添加了注释

    让oldQuestionIndex=-1;
    const headerQuestions=document.getElementById('header-questions');
    const answerA=document.getElementById('a');
    const answerB=document.getElementById('b');
    const answerC=document.getElementById('c');
    const answerD=document.getElementById('d');
    const btnSubmit=document.querySelector('.btn submit');
    康斯特奎扎尔=[
    {
    问题:“2019年最常用的编程语言是什么?”,
    答案:['Java','C','Python','JS']
    },
    {
    问题:“谁是美国总统?”,
    答案:[‘弗洛林流行音乐’、‘乔·拜登’、‘伊万·萨尔达诺’、‘米海·安德烈’]
    },
    {
    问题:“HTML代表什么?”,
    答复:[
    “超文本标记语言”,
    “级联样式表”,
    “Jason对象表示法”,
    “直升机码头摩托艇兰博基尼”
    ]
    },
    {
    问题:“JavaScript发布于哪一年?”,
    答案:[‘1996’、‘1995’、‘1994’、‘以上均无’]
    }
    ];
    功能演示回答(回答){
    让arrAnswerVaranty=['a','b','c','d'];
    for(quizArr中的const id){
    document.getElementById(arrAnswerVaranty[id]).innerHTML=answers[id];
    }
    常数radioArr=docum