-1
Javascriptを使用してクイズのドロップダウンリストフォームを確認およびリセットするには、2つのボタンを追加する必要があります。 [マイベリファイ]ボタンは機能しますが、スコアが表示されたウィンドウを表示する必要があります。すべてのフォームを最初のオプションにリセットする関数を書く方法は?あなたはこのようなJavaScriptでselect要素の値を設定することができますJavascriptを使用してSubmitおよびResetボタンをドロップダウンリストフォーム
<script>
function finalScore() {
var i = 0;
var select1 = document.getElementById("form1");
var answer1 = select1.options[select1.selectedIndex].value;
if(answer1 == "right"){
i++;
}
var select2 = document.getElementById("form2");
var answer2 = select2.options[select2.selectedIndex].value;
if(answer2 == "right"){
i++;
}
var select3 = document.getElementById("form3");
var answer3 = select3.options[select3.selectedIndex].value;
if(answer3 == "right"){
i++;
}
var select4 = document.getElementById("form4");
var answer4 = select4.options[select4.selectedIndex].value;
if(answer4 == "right"){
i++;
}
var select5 = document.getElementById("form5");
var answer5 = select5.options[select5.selectedIndex].value;
if(answer5 == "right"){
i++;
}
var select6 = document.getElementById("form6");
var answer6 = select6.options[select6.selectedIndex].value;
if(answer6 == "right"){
i++;
}
var select7 = document.getElementById("form7");
var answer7 = select7.options[select7.selectedIndex].value;
if(answer7 == "right"){
i++;
}
var select8 = document.getElementById("form8");
var answer8 = select8.options[select8.selectedIndex].value;
if(answer8 == "right"){
i++;
}
var select9 = document.getElementById("form9");
var answer9 = select9.options[select9.selectedIndex].value;
if(answer9 == "right"){
i++;
}
var select10 = document.getElementById("form10");
var answer10 = select10.options[select9.selectedIndex].value;
if(answer10 == "right"){
i++;
}
document.getElementById("scoreDisplay").innerHTML = i;
}
</script>
<body>
<div class="bloc1">
Select the word which does not belong to this food group from the selection list:
</div>
<div class="bloc2">
</br>
<form>
1. What food doesn’t belong to this food group?
</br>
<select id="form1">
<option value="wrong">Banana</option>
<option value="right">Beef</option>
<option value="wrong">Peach</option>
<option value="wrong">Nectarines</option>
<option value="wrong">Plums</option>
<option value="wrong">Tangerines</option>
</select>
</form></br>
<form>
2. The food group in question 1 is:
</br>
<select id="form2">
<option value="wrong">Meat, Poultry, Fish, Beans, Eggs and Nuts Group</option>
<option value="wrong">Fats, Oils and Sweets Group</option>
<option value="right">Fruit Group</option>
<option value="wrong">Bread, Cereal, Rice and Pasta Group</option>
<option value="right">Milk, Yogurt and Cheese Group</option>
</select>
</form></br>
<form>
3. What food doesn’t belong to this food group?
</br>
<select id="form3">
<option value="wrong">Chicken</option>
<option value="wrong">Steak</option>
<option value="wrong">Lamb</option>
<option value="wrong">Crab</option>
<option value="right">Kiwi</option>
</select>
</form></br>
<form>
4. The food group in question 3 is:
</br>
<select id="form4">
<option value="wrong">Bread, Cereal, Rice and Pasta Group</option>
<option value="right">Meat, Poultry, Fish, Beans, Eggs and Nuts Group</option>
<option value="wrong">Vegetable Group</option>
<option value="wrong">Milk, Yogurt and Cheese Group</option>
<option value="wrong">Fats, Oils and Sweets Group</option>
</select>
</form></br>
<form>
5. What food doesn’t belong to this food group?
</br>
<select id="form5">
<option value="right">Apricot</option>
<option value="wrong">Squash</option>
<option value="wrong">Zucchini</option>
<option value="wrong">Potato</option>
<option value="wrong">Broccoli</option>
</select>
</form></br>
<form>
6. The food group in question 5 is:
</br>
<select id="form6">
<option value="wrong">Meat, Poultry, Fish, Beans, Eggs and Nuts Group</option>
<option value="wrong">Fruit Group</option>
<option value="right">Vegetable Group</option>
<option value="wrong">Fats, Oils and Sweets Group</option>
<option value="wrong">Bread, Cereal, Rice and Pasta Group</option>
</select>
</form></br>
<form>
7. What food doesn’t belong to this food group?
</br>
<select id="form7">
<option value="wrong">Chocolate milk</option>
<option value="wrong">Cream cheese</option>
<option value="wrong">Ice cream</option>
<option value="right">Salad dressing</option>
<option value="wrong">Yogurt</option>
</select>
</form></br>
<form>
8. The food group in question 7 is:
</br>
<select id="form8">
<option value="wrong">Fruit Group</option>
<option value="right">Dairy Group</option>
<option value="wrong">Vegetable Group</option>
<option value="wrong">Meat, Poultry, Fish, Beans, Eggs and Nuts Group</option>
<option value="wrong">Fats, Oils and Sweets Group</option>
</select>
</form></br>
<form>
9. What food doesn’t belong to this food group?
</br>
<select id="form9">
<option value="wrong">Cookies</option>
<option value="wrong">Candy</option>
<option value="right">Salad dressing</option>
<option value="wrong">Cherries</option>
<option value="wrong">Butter</option>
</select>
</form></br>
<form>
10. The food group in question 9 is:
</br>
<select id="form10">
<option value="wrong">Dairy Group</option>
<option value="wrong">Vegetable Group</option>
<option value="wrong">Meat, Poultry, Fish, Beans, Eggs and Nuts Group</option>
<option value="wrong">Bread, Cereal, Rice and Pasta Group</option>
<option value="right">Fats, Oils and Sweets Group</option>
</select>
</form>
</div>
<div class="bloc3">
<input type="button" value="Submit" onclick="finalScore()">
<input type="button" value="Reset all" onclick="clearall()">
</br></br>
Questions answered right:
<div id="scoreDisplay">score goes here</div>
</body>
</html>
</div>
クラス名を与え、そのクラスの各要素をループしますか? – NewToJS
ここに例があります:https://jsfiddle.net/cau7uzko/ – NewToJS
すべての答えにクラスを使用することもできます。つまり、getElementsById()またはif文を複製する必要はありません。 – NewToJS