gpt4 book ai didi

javascript - 如何在此 JS 测验中打印提交时所有字段的内容?

转载 作者:行者123 更新时间:2023-12-04 08:36:33 25 4
gpt4 key购买 nike

我做了一个 JS/HTML 测验,我希望在文本字段中输入的所有单词在提交时显示在页面底部。
错误出在 showResults 函数中,它没有按照我的预期工作。我是使用 querySelector 的初学者,但我想要做的是使用变量 answerContainers 仅存储 quizContainer 的 .answers 部分,然后使用 toSearch 变量仅存储从 answerContainers 提交的答案的值。最后,我想以字符串形式将 toSearch 的内容打印到屏幕上。
这是我的代码:

var quizContainer = document.getElementById('quiz');
var resultsContainer = document.getElementById('results');
var submitButton = document.getElementById('submit');

var myQuestions = ["1. What is your dream destination?",
"2. If you could have one wish right now, what would it be?",
"3. What are your career goals?",
"4. Name an artist whose music you enjoy.",
"5. What are your hobbies?",
"6. Name a few public figures you admire.",
"7. Who is your favourite actor?",
"8. Which family member do you love the most?",
"9. If you could have any animal as your pet, what would it be?",
"10. Name a movie you’ve been planning to watch but haven’t yet had the time.",
"11. What kind of weather do you like the most?",
"12. Name a book or movie that you’ve always loved."];

function showQuestions(myQuestions, quizContainer){
var output = [];
for(var j = 0; j <= 11; j++)
{
var answer = '<label>'
+ '<input type="text" name=""+j+"" id=""+j+"">'
+ '</label>';
output.push(
'<div class="question">' + myQuestions[j] +'</div>'
+ '<div class="answers">' + answer + '</div>'
);
}
quizContainer.innerHTML = output.join("");

}

function showResults(questions, quizContainer, resultsContainer){
var answerContainers = quizContainer.querySelectorAll('.answers');

var toSearch = [];

for(var i=0; i <= 11; i++){
toSearch.push(answerContainers[i].querySelector('input[name=""+i+""]')).value;
}
resultsContainer.innerHTML = toSearch.toString();


}
<!DOCTYPE html>
<html>
<head>
<h2>Interests Questionnaire</h2>
<h4>Answer in 1-5 words each.</h4>
<br>
<link rel="stylesheet" href="test.css">
</head>
<body>
<div id="quiz"></div>
<div id="results"></div>
<script src = "test.js"></script>
<script> showQuestions(myQuestions, quizContainer); </script>
<input type="button" value = "Submit" id = "submit" onclick = "showResults(myQuestions, quizContainer, resultsContainer)">
</body>
</html>

在其当前形式中,代码给出错误“JavaScript 错误:TypeError:quizContainer 在第 52 行为 null”。我做错了什么,我该如何解决?

最佳答案

您的代码中有一些错误 - 错位的括号之类的。我发现/修复了那些阻止成功的问题,并得到了显示在字符串中的答案。你应该可以从这里拿走它。
错误列表:
(1) toSearch.append()不正确 - 使用 toSearch.push()(2) 您不能拥有 nameid以数字开头的属性。
(3) 这一行有几个地方不正确:toSearch.append(answerContainers[i].querySelector('input[name=""+i+""]')).value;应该:toSearch.push(answerContainers[i].querySelector('input[name="a'+i+'"]').value);

var quizContainer = document.getElementById('quiz');
var resultsContainer = document.getElementById('results');
var submitButton = document.getElementById('submit');

var myQuestions = ["1. What is your dream destination?",
"2. If you could have one wish right now, what would it be?",
"12. Name a book or movie that you’ve always loved."];

function showQuestions(myQuestions, quizContainer){
var output = [];
for(let j=0; j <= myQuestions.length-1; j++){
var answer = `
<label>
<input type="text" name="a${j}" id="a${j}">
</label>`;
output.push(
`<div class="question">${myQuestions[j]}</div>
<div class="answers">${answer}</div>`
);
}
quizContainer.innerHTML = output.join("");

}

function showResults(myQuestions, quizContainer, resultsContainer){
var answerContainers = quizContainer.querySelectorAll('.answers');
// console.log(answerContainers);

var toSearch = [];

for(var i=0; i <= myQuestions.length-1; i++){
//console.log(answerContainers[i].querySelector('input[name="a'+i+'"]').value);
toSearch.push(answerContainers[i].querySelector('input[name="a'+i+'"]').value);
var x = 0;
}
resultsContainer.innerHTML = JSON.stringify(toSearch);

//userAnswer = (answerContainers[i].querySelector('input[name=question'+i+']:checked')||{}).value;
}

showQuestions(myQuestions, quizContainer);
    <h2>Interests Questionnaire</h2>
<h4>Answer in 1-5 words each.</h4>
<br>
<div id="quiz"></div>
<div id="results"></div>
<input type="button" value = "Submit" id = "submit" onclick = "showResults(myQuestions, quizContainer, resultsContainer)">

关于javascript - 如何在此 JS 测验中打印提交时所有字段的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64770249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com