Javascript 使用预填充java脚本数组动态生成和处理表单
我有一系列问题(Q[I]),每个问题有四个选项(op[])。 我已经从mysql将数据填充到其中Javascript 使用预填充java脚本数组动态生成和处理表单,javascript,arrays,forms,timer,radio,Javascript,Arrays,Forms,Timer,Radio,我有一系列问题(Q[I]),每个问题有四个选项(op[])。 我已经从mysql将数据填充到其中 我希望生成一个动态表单,下面是它的代码,它不工作,让我知道如何进行 我想做一个类似这样的测验 以下是我尝试的代码: for(i=1;i<=10;i++) { <form name="Quiz Test"> document.write(Q[i]+"</br>"); <input type="radio" n
for(i=1;i<=10;i++)
{
<form name="Quiz Test">
document.write(Q[i]+"</br>");
<input type="radio" name="choice" value=op1[i]> document.write(op1[i])<br>
<input type="radio" name="choice" value=op2[i]> document.write(op2[i])<br>
<input type="radio" name="choice" value=op3[i]> document.write(op3[i])<br>
<input type="radio" name="choice" value=op4[i]> document.write(op4[i])<br>
//<input type="submit" onclick="get_radio_value()">
</form>
}
for(i=1;i用于填充数组
<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>
.questionDiv{显示:无}
//下面的数组当然可以在服务器上填充。
//只需将数组转储到JSON即可
//变量测验=
变量测验=[
{“Q”:“如何…”,“A”:[“一”、“二”、“三”、“四”],
{“Q”:“如何…”,“A”:[“一”、“二”、“三”、“四”],
{“Q”:“如何…”,“A”:[“一”、“二”、“三”、“四”],
{“Q”:“如何…”,“A”:[“一”、“二”、“三”、“四”],
{“Q”:“如何…”,“A”:[“一”、“二”、“三”、“四”]}
];//注意最后一个字符后面没有逗号
函数下一步(idx){
document.getElementById('Q'+idx).style.display='none';
document.getElementById('Q'+(idx+1)).style.display='block';
}
window.onload=function(){
var html=“”;
对于(var i=0,n=quick.length;我想问题是我不想自己填充数组。我希望这个过程是动态的。如前所述,我有mysql数据库,我从中随机填充Q和选项数组。这就是问题的原因。好吧,你认为这是正确的方法还是使用jquery会让事情变得更简单。谢谢你的代表可以使用lyJQuery,但会有完全相同的问题,即填充数组。因此,我给你的数组在服务器上填充起来非常容易。PHP甚至有JSON,我已经填充了数组。Q[]中的问题和op1[],op2,op3,op4中的选项,以及ans[]中的答案数组。我想计算并报告客户端本身的结果(使用一些点击处理程序)。接下来我不明白的是如何填写var测验[]。从我的5个数组中检查答案。请推荐任何好的来源(不是很详细)这可能有助于理解这些内容。我是一个新手。非常感谢!!我建议你用PHP标签问一个新问题-我是一个javascripter,从数组中输出我建议的数组对于一个PHP用户来说是微不足道的,例如使用foreach或explode的一些组合。将结果发送到服务器并再次检查你的answer数组。你不应该在网页的源代码中有答案,以避免作弊