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;
}

Document.form.grades [x] [3] .value domオブジェクトにアクセスするさまざまな方法を試しましたが、配列参照がオブジェクトではないのは気に入らない。私の最近の試みは、ボタンに.ID値を追加し、GetElementByID()を使用しようとすることでしたが、それも機能しませんでした。

私はJSの初心者なので、できる場所で支援してください。ティア!

役に立ちましたか?

解決

このような : document.form[grades[x][3]].value ?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top