单击按钮时功能不运行?

时间:2017-02-08 15:47:39

标签: javascript

我的页面上有一个按钮,用于清除文档中的所有textareas,但是当我单击所述按钮时,似乎该功能没有被运行。我已经尝试在文档中添加一个按钮监听器,但这似乎仍然无法正常工作,我不明白问题是什么,因为警报的线路不均匀运行。



    
    function markForm(tagID) {
        var x = document.getElementById(tagID)
        var pre = document.createElement("pre");
        x.appendChild(pre);
        var SectionDetails = [
            { name: "Dynamic Table", maxMarks: 20},
            { name: "IntelliJ usage", maxMarks: 10},
            { name: "Calendar Controller", maxMarks: 30},
            { name: "Active Form", maxMarks: 20},
            { name: "Object Database", maxMarks: 20}
        ];
        var table = pre.appendChild(document.createElement("table"));
        var tableHeader = table.appendChild(document.createElement("thead"));
        var tableRow = tableHeader.appendChild(document.createElement("tr"));
        var headRow = tableRow.appendChild(document.createElement("th"));
        headRow.appendChild(document.createTextNode("Section"));
        headRow = tableRow.appendChild(document.createElement("th"));
        headRow.appendChild(document.createTextNode("Max Mark"));
        headRow = tableRow.appendChild(document.createElement("th"));
        headRow.appendChild(document.createTextNode("Comments"));
        headRow = tableRow.appendChild(document.createElement("th"));
        headRow.appendChild(document.createTextNode("Mark"));
        var resultsRows = table.appendChild(document.createElement("tbody"));
        for (var i = 0; i < SectionDetails.length; i++) {
            tableRow = resultsRows.appendChild(document.createElement("tr"));
            var tableData = tableRow.appendChild(document.createElement("td"));
            tableData.appendChild(document.createTextNode(SectionDetails[i].name));
            tableData = tableRow.appendChild(document.createElement("td"));
            tableData.appendChild(document.createTextNode(SectionDetails[i].maxMarks));
            tableData = tableRow.appendChild(document.createElement("td"));
            var tarea = document.createElement("textarea");
            tarea.value = "Enter Comments";
            tableData.appendChild(tarea);
            tableData = tableRow.appendChild(document.createElement("td"));
            var dropDown = document.createElement("select");
            dropDown.name = SectionDetails[i].name;
            for (var j = 0; j < (SectionDetails[i].maxMarks + 1); j++) {
                var listItem = new Option(j);
                listItem.value=j;
                dropDown.appendChild(listItem);
            }
            tableData.appendChild(dropDown);
        }
        var h2 = document.createElement("h2");
        h2.innerHTML = "Total Marks: " + document.createTextNode(totalMarks);
        x.appendChild(h2);
    }
    
    function totalMarks() {
        var total = 0;
        for (var a = 0; a < SectionDetails.length; a++) {
            var e = document.getElementById(SectionDetails[a].name);
            var total = total + Number(e.options[e.selectedIndex].value);
        }
        return total;
    }
    
    function clear() {
        var textareas = documents.getElementsByTagName("textarea");
        console.log(textareas);
        for (var i = 0; i < textareas.length; i++) {
            textareas[i].value="Enter Comments";
        }
        alert("Fields Cleared");
    }
&#13;
    <body onload="markForm('conversion')">
    <h2>Marking Form: </h2>
    <p id='conversion'>
    
    </p>
    <p id='clear'>
        <input type="button" id="but" value="Clear" onclick="clear()"/>
    </p>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

你的clear()函数似乎有错误。它应该是document.getElementsByTagName()。而你的是“文件” - 同样清楚是其他地方使用的一个词:document.clear