JavaScript未定义错误-从表单获取值

时间:2018-08-08 15:06:45

标签: javascript forms jsp undefined

我是初学者!我开始学习javascript,但是有一个错误,我找不到解决方法。

  

未捕获的TypeError:无法读取未定义的属性“值”       正确(homework.jsp:50)       在HTMLInputElement.onclick(homework.jsp:63)

当我按下“完成”按钮时会发生这种情况。我想从表单中获取价值。但是该值始终是“未定义的”,我无法将其更改为Number或String。也许我未能获得价值。你能帮我吗?

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<h2>Homework</h2>
<form name="gugu">
<script type="text/javascript">
    //How many questions?
    var size=eval(window.prompt("How many questions?"));
    var a=new Array(size);
    var b=new Array(size);
    var ans=new Array(size);
    var rst=new Array(size);
    var count=0;

    //Start
    gugugu();
    function gugugu() {
        document.write("<table border='1'>");
        for (var i=0; i<size+1; i++) {
            if(i<size) {
                document.write("<tr>");
                document.write("<td>");
                    a[i]=parseInt(Math.random()*9)+1;
                    b[i]=parseInt(Math.random()*9)+1;
                    rst[i]=a[i]*b[i];
                    document.write(a[i]+"*"+b[i]+"=");
                document.write("</td>");
                document.write("<td>");
                    //I can't get the value!
                    var str="<input type='text' name='"+i+"'>";
                    document.write(str);
                document.write("</td>");
                document.write("</tr>");
            } else {
                document.write("<tr>");
                document.write("<td colspan='2' align='center'>");
                    document.write("<input type='button' value='done' onclick='correct()'>");
                    document.write("<input type='button' value='reset' onclick='reset()'>");
                document.write("</td>");
                document.write("</tr>");
            }
        }
        document.write("</table>");
        document.write("count : ");
        document.write("<input type='text' value='' name='count' readonly>");
        document.write("<br>");
        document.write("note : ");
        document.write("<input type='text' value='' name='note' readonly>");
    }
    function correct() {
        for (var i=0; i<size;i++) {
            //Here I tried putting values into an Array, but 'undefined' happens!
            ans[i]=String(document.gugu.i.value);
            document.write(typeof(ans[i])+","+rst[i]+"<br>");
            if(ans[i]==rst[i]) {
                count=count+1;
            }
        }
        document.gugu.count.value=eval(count);
        document.gugu.note.value=eval(count*10);
    }
    function reset() {
        clear();
        gugugu();
    }
</script>
</form>

2 个答案:

答案 0 :(得分:0)

我认为您无法访问此类document.something的html元素。选中this,查看使用JavaScript访问html元素的不同方法。

答案 1 :(得分:0)

“ gugu”的格式名称document.gugu未定义,因为它不是全局变量,因此获得以下格式:document.getElementsByName(“ gugu”);

ans[i]=String(document.gugu.i.value);

然后在获取表单(HTMLElement []类型)之后,您试图获取一行吗?不能执行FORM.i,“ i”在这里没有上下文,可以从数组ans []中进行访问,这样,您就可以通过输入的名称(设置为名称的数字)从输入中获取值:

ans[i] = String(document.getElementsByName("" + i)[0].values[i]);

因此,在此处和此处进行一些更改即可起作用:

<h2>Homework</h2>
<form name="gugu">
<script type="text/javascript">
    //How many questions?
    var size = eval(window.prompt("How many questions?"));
    var a = new Array(size);
    var b = new Array(size);
    var ans = new Array(size);
    var rst = new Array(size);
    var count = 0;

    //Start
    gugugu();
    function gugugu() {
        document.write("<table border='1'>");
        for (var i = 0; i < size + 1; i++) {
            if (i < size) {
                document.write("<tr>");
                document.write("<td>");
                a[i] = parseInt(Math.random() * 9) + 1;
                b[i] = parseInt(Math.random() * 9) + 1;
                rst[i] = a[i] * b[i];
                document.write(a[i] + "*" + b[i] + "=");
                document.write("</td>");
                document.write("<td>");
                var str = "<input type='text' name='in" + i + "'>";
                document.write(str);
                document.write("</td>");
                document.write("</tr>");
            } else {
                document.write("<tr>");
                document.write("<td colspan='2' align='center'>");
                document.write("<input type='button' value='done' onclick='correct()'>");
                document.write("<input type='button' value='reset' onclick='reset()'>");
                document.write("</td>");
                document.write("</tr>");
            }
        }
        document.write("</table>");
        document.write("count : ");
        document.write("<input type='text' value='' name='count' readonly>");
        document.write("<br>");
        document.write("note : ");
        document.write("<input type='text' value='' name='note' readonly>");
    }
    function correct() {
        console.log("correct");
        for (var i = 0; i < size; i++) {
            ans[i] = parseInt(document.getElementsByName("in" + i)[0].value);
            console.log(ans[i]);
            console.log(rst[i]);
            if (ans[i] === rst[i]) {
                count = count + 1;
            }
        }
        console.log(count);

        document.getElementsByName("count")[0].value = count;
        document.getElementsByName("note")[0].value = (count * 10);
    }
    function reset() {
        clear();
        gugugu();
    }
</script>
</form>