JavaScript/DOM:在动态创建的文本对象上,如何通过DOM引用该值?

StackOverflow https://stackoverflow.com/questions/3996689

  •  10-10-2019
  •  | 
  •  

我有一个看起来像这样的数组:

grades[0] = ["A", 4.0, 0, "a"];
grades[1] = ["A-", 3.67, 0, "a-minus"];
grades[2] = ["B+", 3.33, 0, "b-plus"];
grades[3] = ["B", 3.0, 0, "b"];
grades[4] = ["B-", 2.67, 0, "b-minus"];
grades[5] = ["C+", 2.33, 0, "c-plus"];
grades[6] = ["C", 2.0, 0, "c"];
grades[7] = ["C-", 1.67, 0, "c-minus"];
grades[8] = ["D+", 1.33, 0, "d-plus"];
grades[9] = ["D", 1.0, 0, "d"];
grades[10] = ["D-", 0.67, 0, "d-minus"];
grades[11] = ["F", 0.0, 0, "f"];

以及看起来像这样的动态形式:

function loadButtons() {
    var myDiv = document.getElementById("divMain");
    var myTable = document.createElement("table");
    myTable.id = "grades";
    myTable.border = 1;
    myTable.padding = 5;
    var myTbody = document.createElement("tbody");

    for (var i=0; i < grades.length; i++) {
        var myTr = document.createElement("tr");
        var myTd1 = document.createElement("td");
        var myTd2 = document.createElement("td");
        var myTd3 = document.createElement("td");
        var myTd4 = document.createElement("td");

        var myButton = document.createElement('input');
        myButton.type = 'button';
        myButton.name = grades[i][1];
        myButton.value = "+";
        myButton.onclick = function (){addGrade(this)};
        myTd1.appendChild(myButton);
        myTr.appendChild(myTd1);

        myTd2.appendChild(document.createTextNode(grades[i][1]));
        myTr.appendChild(myTd2);

        myTd3.appendChild(document.createTextNode(grades[i][2]));
        myTr.appendChild(myTd3);

        var myButton = document.createElement('input');
        myButton.type = 'text';
        myButton.name = grades[i][1];
        myButton.id = grades[i][3];
        myButton.size = "4";
        myButton.onblur = function (){addGradeX(this)};
        myTd4.appendChild(myButton);
        myTr.appendChild(myTd4);

        myTbody.appendChild(myTr);
        myTable.appendChild(myTbody);
        myDiv.appendChild(myTable);
    }
}

而且我尝试使用以下方式更新TD4(最后一个按钮)中的按钮的值:

function writeGrades() {    
    clearForm();

    for (var x=0; x < grades.length; x++) {
        if (grades[x][2] > 0) {
            for (var i=1; i <= grades[x][2]; i++) {
                sGradeLog += grades[x][0] + ",";
                sGradeValLog += grades[x][1] + ",";

                iCumTotal += grades[x][1];
                iNumOfGrades += 1;
            }
        }
    }


    for (var x=0; x < grades.length; x++) {  //**this loop is where I'm trying to make this happen**
        var myVal = grades[x][3];
        var myEl = getElementById(document.form.myVal.value);
        document.form.myEl.value = grades[x][2];
    }

    iGPA = iCumTotal / iNumOfGrades;                

    document.form.GPA.value = iGPA.toFixed(3);
    document.form.cumTotal.value = iCumTotal.toFixed(3);
    document.form.numOfGrades.value = iNumOfGrades;

    document.form.gradeLog.value = sGradeLog;
    document.form.gradeValLog.value = sGradeValLog;
}

我尝试了许多不同的访问文档的方法。form.grades[x] [3]。值dom对象,但它不喜欢阵列引用不是对象。我最近的尝试是将.ID值添加到按钮中,然后尝试使用getElementById(),但这也没有工作。

我是JS的新手,所以请尽可能地帮助您。蒂亚!

有帮助吗?

解决方案

像这样 : document.form[grades[x][3]].value ?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top