尝试重置javascript表单使表单消失

尝试重置javascript表单使表单消失,javascript,forms,reset,Javascript,Forms,Reset,我创建了一个表格,从16个问题中随机选择12个T/F问题,以随机顺序显示,并在最后对其进行评分。问题是我无法在刷新时清除表单。当我尝试几乎任何方法时,形式、问题和一切都消失了 但是,重置按钮确实起作用 以下是表格: <script type="text/javascript"> //Define Questions Array var q = new Array(); q[0] = "I have fingers"; q[1] = "I have toes";

我创建了一个表格,从16个问题中随机选择12个T/F问题,以随机顺序显示,并在最后对其进行评分。问题是我无法在刷新时清除表单。当我尝试几乎任何方法时,形式、问题和一切都消失了

但是,重置按钮确实起作用

以下是表格:

<script type="text/javascript">
//Define Questions Array
var q = new Array();
    q[0] = "I have fingers";
    q[1] = "I have toes";
    q[2] = "I have gills";
    q[3] = "I have an appendix";
    q[4] = "I can swim";
    q[5] = "I can fly";
    q[6] = "I am a human";
    q[7] = "I own a PC";
    q[8] = "I own a Mac";
    q[9] = "I own a home";
    q[10] = "I own property in Hawaii";
    q[11] = "I speak english";
    q[12] = "I speak Cantonese";
    q[13] = "I have my driver's license";
    q[14] = "I have my pilot's license";
    q[15] = "I am in the military";
//Define Answers Array
var a = new Array();
    a[0] = "True";
    a[1] = "True";
    a[2] = "False";
    a[3] = "False";
    a[4] = "True";
    a[5] = "False";
    a[6] = "True";
    a[7] = "True";
    a[8] = "True";
    a[9] = "False";
    a[10] = "True";
    a[11] = "True";
    a[12] = "False";
    a[13] = "True";
    a[14] = "False";
    a[15] = "False";

var order = 0; //used to count things
var rand; //random number
var nQ = new Array(16); //re-ordered questions
var nA = new Array(16); //matching re-ordered answers
var uA = new Array(12); //user's answers
var x = 1; //counting variable
var s; //counting variable
var score = 0; //user's score

//This function records the user's input
function recordIt(n,TF)
{
    uA[n] = TF;
}

//This function scores the user's input and display's how many they got correct
function scoreIt()
{
for (s in uA)
{
    if ( uA[s] == nA[s])
    {
        score++;
    }
}
alert(score);
}

//This function checks to see if all of the questions have re-ordered
function allX(arr)
{
    var count = 0;
    while ( count != 16)
    {
        if (arr[count] == "X")
        {
            count++;
        }
        else
        {
            break;
        }
    }
    if (count == 16)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//This randomly organizes the questions and answers
while (allX(q) == false)
{
    rand = Math.floor(Math.random()*16)
    if (q[rand] != "X")
    {
        nQ[order] = q[rand];
        nA[order] = a[rand];
        q[rand] = "X";
        a[rand] = "X";
        order++;
    }
}

//This is the actual form that picks the first 12 questions from the nQ (new questions) array
document.write("<form name=oquiz>");
while (x < 13)
{
document.write(x + ". " + nQ[x]);
document.write("<br/>");
document.write("<input type=radio name=\"" + "q" + x + "\" value=\"True\" onClick=\"recordIt(" + x + ",this.value)\"> True");
document.write("<br/>");
document.write("<input type=radio name=\"" + "q" + x + "\" value=\"False\" onClick=\"recordIt(" + x + ",this.value)\"> False");
document.write("<br/>");
x++;
}
document.write("<input type=\"button\" value=\"Reset Form\" onClick=\"this.form.reset()\" />")
document.write("<input type=\"button\" value=\"Score this quiz\" onClick=\"scoreIt()\" />")
document.write("</form>");
</script>

//定义问题数组
var q=新数组();
q[0]=“我有手指”;
q[1]=“我有脚趾”;
q[2]=“我有鳃”;
q[3]=“我有附录”;
q[4]=“我会游泳”;
q[5]=“我会飞”;
q[6]=“我是一个人”;
q[7]=“我有一台电脑”;
q[8]=“我拥有一台Mac电脑”;
q[9]=“我有一个家”;
q[10]=“我在夏威夷拥有财产”;
q[11]=“我会说英语”;
q[12]=“我会说广东话”;
q[13]=“我有驾驶执照”;
q[14]=“我有飞行员执照”;
q[15]=“我在军队”;
//定义答案数组
var a=新数组();
a[0]=“真”;
a[1]=“真”;
a[2]=“假”;
a[3]=“假”;
a[4]=“正确”;
a[5]=“假”;
a[6]=“正确”;
a[7]=“正确”;
a[8]=“正确”;
a[9]=“假”;
a[10]=“正确”;
a[11]=“正确”;
a[12]=“假”;
a[13]=“正确”;
a[14]=“假”;
a[15]=“假”;
var阶数=0//用来数东西的
var rand//随机数
var nQ=新阵列(16)//重新排序的问题
var nA=新阵列(16)//匹配重新排序的答案
var uA=新阵列(12)//用户的答案
var x=1//计数变量
var s//计数变量
var得分=0//用户分数
//此函数记录用户的输入
函数recordIt(n,TF)
{
uA[n]=TF;
}
//此函数对用户的输入进行评分,并显示正确的数量
函数scoreIt()
{
适用于(uA中的s)
{
如果(uA[s]==nA[s])
{
分数++;
}
}
警惕(得分);
}
//此函数检查所有问题是否已重新排序
函数allX(arr)
{
var计数=0;
而(计数!=16)
{
如果(arr[计数]=“X”)
{
计数++;
}
其他的
{
打破
}
}
如果(计数=16)
{
返回true;
}
其他的
{
返回false;
}
}
//这将随机组织问题和答案
while(allX(q)=false)
{
rand=Math.floor(Math.random()*16)
如果(q[rand]!=“X”)
{
nQ[订单]=q[兰德];
nA[顺序]=a[兰德];
q[rand]=“X”;
a[rand]=“X”;
订单++;
}
}
//这是从nQ(新问题)数组中挑选前12个问题的实际形式
文件。填写(“”);
而(x<13)
{
文件。写(x+“+nQ[x]);
文件。写(“
”); 文件。填写(“真实”); 文件。写(“
”); 文件。填写(“虚假”); 文件。写(“
”); x++; } 文件。写(“”) 文件。写(“”) 文件。填写(“”);

暗示将是了不起的。谢谢

为什么不使用带有重置类型的输入

<input type="reset" />

您遇到的问题是文档。write可能正在关闭表单本身,因为您没有向页面写入正确的html

您应该只创建一个带有html标记的字符串,然后将其写在一行中

var str = "<form>";
str += "<input ... />";
str += "</form>";
document.write(str);
var-str=”“;
str+=”;
str+=”;
文件编写(str);

更好的方法是,学会使用和。

不使用document.write您可以将所有输出设置为字符串,然后将正文或HTML中的元素设置为该输出


示例:

我猜您正在丢失表单,因为它是使用
document.write创建的。。直接进入
DOM
,您可能会有更好的运气。尝试使用Javascript模板引擎获得更干净的代码,避免字符串、HTML和Javascript的混合。除非你有感情用事,否则放下document.write(写)我认为document.write是个问题,但我不知道没有它,如何在nQ数组中显示问题。建议?使用
.createElement()
.appendChild()
代替,如@epascarello所述。