您好,我正在努力寻找让简单的在线测验将用户结果发送到指定电子邮件的最佳方法。我对 Javascript 相当陌生,如果有人能指出我正确的方向,我将不胜感激。
var allQuestions = [{question: "2 + 2", choices: ["4", "8", "10", "2"], correctAnswer:0},
{question: "6 + 3", choices: ["7", "3", "5", "9"], correctAnswer:3},
{question: "4 + 4", choices: ["8", "7", "2", "9"],
correctAnswer:0},
{question: "5 + 0", choices: ["4", "5", "3", "9"], correctAnswer:1},];
//you can access checkbox name through an array
//match array number to number in allQuestions array
var questionNum = 0;
var scoreNum = 0;
var makeQuestions = "";
$(document).ready(function() {
makeQuestions = function() {
if(questionNum === allQuestions.length){
$("input[value=SUBMIT]").remove();
$("#questions").text(" Done!Please click the button below to submit your results. Your score is" + " " + scoreNum);
}
else{
$("#questions").text(allQuestions[questionNum].question);
for(var i=0; i<allQuestions[questionNum]['choices'].length;i++){
$('#words').append('<input type="radio" name="buttons">' + allQuestions[questionNum]['choices'][i] + '</input');
}
}
}
makeQuestions();
});
var checkQuestions = function() {
var lenG = document.getElementsByName("buttons").length;
console.log(lenG);
var rightAnswer = allQuestions[questionNum]['correctAnswer'];
for (var i=0; i<lenG; i++){
if (document.getElementsByName("buttons")[i].checked === true) {
console.log(i);
console.log(document.getElementsByName("buttons")[i].checked);
//compare value to what was inputted
if(i === rightAnswer){
scoreNum +=1;
alert("Correct! Your score is" +" " + scoreNum);
}
else{
alert("False! Your score is still"+ " " + scoreNum );
}
}
}
questionNum = questionNum +1;
$("#words").empty();
makeQuestions();
}