从输入字段获取用户输入到js表,通过for循环创建表格单元格

问题描述 投票:0回答:1

我一直在尝试从输入字段中获取用户输入值,以后将其设置为使用javascript创建单元格的汇总表,但似乎无法使其工作。

以下是我的代码:

function summonTable(f) {
    var sLoc = f.countryf.value();
    var eLoc = f.countryt.value();
    var sDate = f.sdate.value();
    var eDate = f.edate.value();

    var div = document.getElementById("summary");

    var magicTable = document.getElementById('summaryTable').querySelectorAll('tbody tr'),
        var row = document.createElement('tr');
        var th = document.createElement('th');
        th.textContent = sDate;
        var td = document.createElement('td');
        td.textContent = sLoc;


            //To state which row and column this cell goes to.
            row.appendChild(th);
            div.appendChild(magicTable);
        });

函数summonTable将采用包含输入字段的争论形式f,我尝试过基本的文本框,复选框,无线电,但都无济于事。变量sLoc,eLoc基本上是国家和sDate的文本,eDate应该是日期。

javascript html
1个回答
0
投票
function summonTable(f) {
    var sLoc = f.countryf.value();
    var eLoc = f.countryt.value();
    var sDate = f.sdate.value();
    var eDate = f.edate.value();

    var div = document.getElementById("summary");
// to get the last row of the table
    var magicTable = document.getElementById('summaryTable').querySelectorAll('tbody tr:last-child'),
        var row = document.createElement('tr');
        var th = document.createElement('th');
        th.textContent = sDate;
        var td = document.createElement('td');
        td.textContent = sLoc;


            //To state which row and column this cell goes to.
            row.appendChild(th);
            magicTable.after(row); //To add the row after the last row
            div.appendChild(magicTable);
        });
© www.soinside.com 2019 - 2024. All rights reserved.