JavaScript – 计算点数不起作用

huangapple go评论54阅读模式
英文:

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:

&lt;fieldset id=&quot;question1&quot;&gt;
	&lt;p style=&quot;font-weight:600;&quot;&gt;What&#39;s your favorite color?&lt;/p&gt;
	&lt;label&gt;&lt;input type=&quot;radio&quot; name=&quot;q1&quot; id=&quot;q1a1&quot; required&gt;Red&lt;/label&gt;&lt;br&gt;
	&lt;label&gt;&lt;input type=&quot;radio&quot; name=&quot;q1&quot; id=&quot;q1a2&quot; required&gt;Blue&lt;/label&gt;&lt;br&gt;
&lt;/fieldset&gt;
&lt;fieldset id=&quot;question2&quot;&gt;
	&lt;p style=&quot;font-weight:600;&quot;&gt;How old are you?&lt;/p&gt;
	&lt;label&gt;&lt;input type=&quot;radio&quot; name=&quot;q2&quot; id=&quot;q2a1&quot;&gt;over 18&lt;/label&gt;&lt;br&gt;
	&lt;label&gt;&lt;input type=&quot;radio&quot; name=&quot;q2&quot; id=&quot;q2a2&quot;&gt;under 18&lt;/label&gt;&lt;br&gt;
&lt;/fieldset&gt;

Name represent question number and id represent question number together with answer number.

Javascript

var points = {
	&quot;q1&quot;: {&quot;q1a1&quot;: 0, &quot;q1a2&quot;: 1},
	&quot;q2&quot;: {&quot;q2a1&quot;: 0, &quot;q2a2&quot;: 3}
};
var totalPoints = 0;

function calculatePoints() {
	for (var i = 1; i &lt;= numQuestions; i++) {
		var selectedAnswer = document.querySelector(&#39;input[name=&quot;q&#39; + i + &#39;&quot;]:checked&#39;).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 = {
    &quot;q1&quot;: {&quot;q1a1&quot;: 0, &quot;q1a2&quot;: 1},
    &quot;q2&quot;: {&quot;q2a1&quot;: 0, &quot;q2a2&quot;: 3}
};
var totalPoints = 0;

function calculatePoints() {
  for (var i = 1; i &lt;= 2; i++) {
    let questionKey = `q${i}`;
    var selectedAnswer = document.querySelector(`input[name=&quot;${questionKey}&quot;]:checked`);

    // that&#39;s what I added
    if (selectedAnswer) {
      totalPoints += points[questionKey][selectedAnswer.id]; // calling .id here
    };
  }
  console.log({ totalPoints });
}

<!-- language: lang-html -->

&lt;fieldset id=&quot;question1&quot;&gt;
    &lt;p style=&quot;font-weight:600;&quot;&gt;What&#39;s your favorite color?&lt;/p&gt;
    &lt;label&gt;&lt;input type=&quot;radio&quot; name=&quot;q1&quot; id=&quot;q1a1&quot; required&gt;Red&lt;/label&gt;&lt;br&gt;
    &lt;label&gt;&lt;input type=&quot;radio&quot; name=&quot;q1&quot; id=&quot;q1a2&quot; required&gt;Blue&lt;/label&gt;&lt;br&gt;
&lt;/fieldset&gt;
&lt;fieldset id=&quot;question2&quot;&gt;
    &lt;p style=&quot;font-weight:600;&quot;&gt;How old are you?&lt;/p&gt;
    &lt;label&gt;&lt;input type=&quot;radio&quot; name=&quot;q2&quot; id=&quot;q2a1&quot;&gt;over 18&lt;/label&gt;&lt;br&gt;
    &lt;label&gt;&lt;input type=&quot;radio&quot; name=&quot;q2&quot; id=&quot;q2a2&quot;&gt;under 18&lt;/label&gt;&lt;br&gt;
&lt;/fieldset&gt;

&lt;button type=&quot;button&quot; onclick=&quot;calculatePoints()&quot;&gt;
  calculate
&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月6日 21:50:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定