我的测验申请有问题。我是新手,我的问题是我不知道如何从JS数组给出输入类型的无线电默认值。因此,当页面加载时,您已经可以看到array [0] postion值作为选项。
HTML code:
<p>
<p id="question"></p>
</p>
<input type="radio" name="quiz" id="ans1" >
<br>
<input type="radio" name="quiz" id="ans2">
<br>
<input type="radio" name="quiz" id="ans3">
<br>
<input type="radio" name="quiz" id="ans4">
<br>
<input type="submit" value="Start" onClick="RadioCheck()">
</form>
JS代码:
<script language="javascript">
var allQuestions = [{
question: "Question1?",
choices: ["Answer1", "Answer2", "Answer3", "Answer4"],
CorrectAnswer:0},
{
question: "Question2?",
choices: ["Answer1", "Answer2", "Answer3", "Answer4"],
CorrectAnswer:0},
{
question: "Question3?",
choices: ["Answer1", "Answer2", "Answer3", "Answer4"],
CorrectAnswer:0},
{
question: "Question4?",
choices: ["Answer1", "Answer2", "Answer3", "Answer4"],
CorrectAnswer:0}
];
var currentQuestion=0;
var CorrectAnswer=0;
function RadioCheck() {
//populate question in HTML
var question = document.getElementById("question");
question.nextSibling.data = allQuestions[currentQuestion].question;
//populate answers in HTML
var ans1 = document.getElementById("ans1");
ans1.nextSibling.data = allQuestions[currentQuestion].choices[0];
var ans2 = document.getElementById("ans2");
ans2.nextSibling.data = allQuestions[currentQuestion].choices[1];
var ans3 = document.getElementById("ans3");
ans3.nextSibling.data = allQuestions[currentQuestion].choices[2];
var ans4 = document.getElementById("ans4");
ans4.nextSibling.data = allQuestions[currentQuestion].choices[3];
var selection = document.getElementsByTagName('input')
if(selection[allQuestions[currentQuestion].CorrectAnswer].checked == true ){
CorrectAnswer ++;
}
currentQuestion ++;
}
</script>
当我第一次单击“开始”按钮时,这是有效的,但我想在页面加载后立即显示Question1和答案。
答案 0 :(得分:0)
您需要为onload事件添加一个函数。
例如,在您的身体声明中,您可以添加以下内容
<body onload="RadioCheck()">
当页面加载功能时,RadioCheck将运行填充表单。
您可以在此处找到有关onload事件的更多信息:http://www.w3schools.com/jsref/event_onload.asp