英文:
Javascript - points calculation doesn't work
问题
I created a simple survey, where every answer to a question gives you different amount of the points. I don't want the points to be in the value itself so I put it into the javascript.
HTML code:
<fieldset id="question1">
<p style="font-weight:600;">What's your favorite color?</p>
<label><input type="radio" name="q1" id="q1a1" required>Red</label><br>
<label><input type="radio" name="q1" id="q1a2" required>Blue</label><br>
</fieldset>
<fieldset id="question2">
<p style="font-weight:600;">How old are you?</p>
<label><input type="radio" name="q2" id="q2a1">over 18</label><br>
<label><input type="radio" name="q2" id="q2a2">under 18</label><br>
</fieldset>
Name represents the question number, and the ID represents the question number together with the answer number.
Javascript
var numQuestions = 2;
var points = {
"q1": {"q1a1": 0, "q1a2": 1},
"q2": {"q2a1": 0, "q2a2": 3}
};
var totalPoints = 0;
function calculatePoints() {
for (var i = 1; i <= numQuestions; i++) {
var selectedAnswer = document.querySelector('input[name="q' + i + '"]:checked').id;
totalPoints += points[selectedAnswer];
}
}
I tried to add the calculation of the points into the listener itself, but it also throws the same error. Just want the number of points being taken from a variable based on the selectedAnswer and calculated and sent to the next page with results.
英文:
I created a simple survey, where every answer to a question gives you different amount of the points. I don't want the points to be in the value itself so I put it into the javascript.
I cannot find the error why in the console the querySelector throws null:
Uncaught TypeError: document.querySelector(...) is null
HTML code:
<fieldset id="question1">
<p style="font-weight:600;">What's your favorite color?</p>
<label><input type="radio" name="q1" id="q1a1" required>Red</label><br>
<label><input type="radio" name="q1" id="q1a2" required>Blue</label><br>
</fieldset>
<fieldset id="question2">
<p style="font-weight:600;">How old are you?</p>
<label><input type="radio" name="q2" id="q2a1">over 18</label><br>
<label><input type="radio" name="q2" id="q2a2">under 18</label><br>
</fieldset>
Name represent question number and id represent question number together with answer number.
Javascript
var points = {
"q1": {"q1a1": 0, "q1a2": 1},
"q2": {"q2a1": 0, "q2a2": 3}
};
var totalPoints = 0;
function calculatePoints() {
for (var i = 1; i <= numQuestions; i++) {
var selectedAnswer = document.querySelector('input[name="q' + i + '"]:checked').id;
totalPoints += points[selectedAnswer];
}
}
I tried to add the calculation of the points into the listener itself but it also throws the same error. Just want the number of points being taken from variable based on the selectedAnswer and calculated and send to the next page with results.
答案1
得分: 1
你在计算分数时甚至在没有选中的复选框上进行了计算,导致找不到 DOM 节点,返回 null,然后在 null 上调用 .id
。所以,你可以要么检查每个“question”是否有答案,要么添加一个检查,看看document.querySelector(...)
是否返回 null。它在代码片段中添加了一个 if 语句,并在条件之后调用了 .id
。
var points = {
"q1": {"q1a1": 0, "q1a2": 1},
"q2": {"q2a1": 0, "q2a2": 3}
};
var totalPoints = 0;
function calculatePoints() {
for (var i = 1; i <= 2; i++) {
let questionKey = `q${i}`;
var selectedAnswer = document.querySelector(`input[name="${questionKey}"]:checked`);
// 这是我添加的部分
if (selectedAnswer) {
totalPoints += points[questionKey][selectedAnswer.id]; // 在这里调用 .id
};
}
console.log({ totalPoints });
}
<fieldset id="question1">
<p style="font-weight:600;">What's your favorite color?</p>
<label><input type="radio" name="q1" id="q1a1" required>Red</label><br>
<label><input type="radio" name="q1" id="q1a2" required>Blue</label><br>
</fieldset>
<fieldset id="question2">
<p style="font-weight:600;">How old are you?</p>
<label><input type="radio" name="q2" id="q2a1">over 18</label><br>
<label><input type="radio" name="q2" id="q2a2">under 18</label><br>
</fieldset>
<button type="button" onclick="calculatePoints()">
calculate
</button>
英文:
You calculate the points even on checkboxes that don't have a checked radiobox -> can't find a DOM Node -> null -> calls .id
on null. So, you either check that every "question" has an answer OR add a check if the document.querySelector(...)
returns null. It added an if-clause in the snippet and called the .id
after the condition.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var points = {
"q1": {"q1a1": 0, "q1a2": 1},
"q2": {"q2a1": 0, "q2a2": 3}
};
var totalPoints = 0;
function calculatePoints() {
for (var i = 1; i <= 2; i++) {
let questionKey = `q${i}`;
var selectedAnswer = document.querySelector(`input[name="${questionKey}"]:checked`);
// that's what I added
if (selectedAnswer) {
totalPoints += points[questionKey][selectedAnswer.id]; // calling .id here
};
}
console.log({ totalPoints });
}
<!-- language: lang-html -->
<fieldset id="question1">
<p style="font-weight:600;">What's your favorite color?</p>
<label><input type="radio" name="q1" id="q1a1" required>Red</label><br>
<label><input type="radio" name="q1" id="q1a2" required>Blue</label><br>
</fieldset>
<fieldset id="question2">
<p style="font-weight:600;">How old are you?</p>
<label><input type="radio" name="q2" id="q2a1">over 18</label><br>
<label><input type="radio" name="q2" id="q2a2">under 18</label><br>
</fieldset>
<button type="button" onclick="calculatePoints()">
calculate
</button>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论