JavaScript 数组以传递 HTML 无线电的默认值
JavaScript array to pass default value for HTML radio
我对测验应用程序有疑问。我是新手,我的问题是我不知道如何从 JS 数组中给输入类型无线电默认值。因此,当页面加载时,您已经可以看到 array[0] 位置值作为选项。
HTML代码:
<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>
这在我第一次单击“开始”按钮时有效,但我想在页面加载后立即显示问题 1 和答案。
您需要为 onload 事件添加一个函数。
例如,在您的正文声明中,您可以添加以下内容
<body onload="RadioCheck()">
页面加载时,RadioCheck 函数将运行 填充您的表单。
您可以在此处找到有关 onload 事件的更多信息:http://www.w3schools.com/jsref/event_onload.asp
我对测验应用程序有疑问。我是新手,我的问题是我不知道如何从 JS 数组中给输入类型无线电默认值。因此,当页面加载时,您已经可以看到 array[0] 位置值作为选项。
HTML代码:
<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>
这在我第一次单击“开始”按钮时有效,但我想在页面加载后立即显示问题 1 和答案。
您需要为 onload 事件添加一个函数。
例如,在您的正文声明中,您可以添加以下内容
<body onload="RadioCheck()">
页面加载时,RadioCheck 函数将运行 填充您的表单。
您可以在此处找到有关 onload 事件的更多信息:http://www.w3schools.com/jsref/event_onload.asp