使用PHP JSON填充数组
<html>
<head>
<style>
.questionDiv { display:none }
</style>
<script type="text/javascript">
// the array below is of course fillable on the server.
// Just dump the array to JSON
// var quiz = <?PHP echo $someJSON ?>
var quiz = [
{"Q":"How ....","A":["one","two","three","four"]},
{"Q":"How ....","A":["one","two","three","four"]},
{"Q":"How ....","A":["one","two","three","four"]},
{"Q":"How ....","A":["one","two","three","four"]},
{"Q":"How ....","A":["one","two","three","four"]}
]; // note no comma after the last
function next(idx) {
document.getElementById('Q'+idx).style.display='none';
document.getElementById('Q'+(idx+1)).style.display='block';
}
window.onload=function() {
var html = "";
for(var i=0,n=quiz.length;i<n;i++) {
html += '<div class="questionDiv" id="Q'+i+'"><span class="question">'+(i+1)+'. '+quiz[i].Q+'</span><br />'
var answer = quiz[i].A;
for (var j = 0, m=answer.length;j<m;j++) {
var ident = 'A'+i+'_'+j;
html += '<input type="radio" name="'+ident+'" id="'+ident+'" value="'+j+'" /><label for="'+ident+'">'+(j+1)+'. '+answer[j]+'</label><br />';
}
if (i<quiz.length-1) html += '<input type="button" onclick="next('+i+')" value="Next"/>';
else html += '<input type="submit" />';
html += '</div>'
}
document.getElementById('quizDiv').innerHTML=html;
document.getElementById('Q0').style.display='block';
}
</script>
</head>
<body>
<form action="answer.php">
<div id="quizDiv">
</div>
</body>
</form>
<html>