var q1textbox = document.getElementsByClassName('q1');
var q2radio = document.getElementsByClassName('q2');
var correct = 0;
var wrong = 0;
var total = 10;
var grade = 0;
function doTheCorrecting() {
// Question one
if (q1textbox.value == 'Scott') {
correct+= 1;
console.log('q1 was correct ' + 'correct=' + correct);
} else {
wrong+= 1;
console.log('q1 is wrong ' + 'wrong=' + wrong);
}
// Question two
if (q2radio.checked = true) {
correct+= 1;
console.log('q2 was correct ' + 'correct=' + correct);
}
if (q2radio.checked = false) {
wrong += 1;
console.log('q2 is wrong ' + 'wrong=' + wrong);
}
// Question three
// Question four
// Question five
grade = total - wrong;
console.log('grade is ' + grade)
}
私が作ったクイズをどのように評価するのかを理解しようとしてきましたが、それを行う方法を理解しようと1時間ほど過ごしました。主なエラーは、私がScottをテキストボックスに入力したとしても、間違って1を追加するということです。これをどうすれば解決できますか?jsで使用するテキストボックスの値/入力を取得するにはどうすればよいですか?
「==」ではなく「==」を使用して比較します。 – Li357
詳細については、複製を参照してください。つまり、 'getElementsByClassName'は複数の要素が同じクラスを持つことができるので、要素のリストを返します。配列の最初の要素にアクセスして、クラスで最初の要素を実際に取得します。 – Li357
** 'document.getElementsByClassName' **は、**' HTMLCollection' **を返します。 –