javascript 如何读取表格内的文本框的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17379112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:05:47  来源:igfitidea点击:

How to read value of Textbox which is inside table

javascripthtml-tabletextbox

提问by Kanishka Gupta

I need to read the value of textbox which is inside the table.

我需要读取表格内文本框的值。

Following is how I create table.

以下是我创建表的方式。

var theader = '<table border = "1" id = "MarksTable">\n';
var tbody = '';

for ( var i = 0; i < total_rows; i++) {
    tbody += '<tr>';
    for ( var j = 0; j < total_col; j++) {
        tbody += '<td name=' + "cell" + i + j + '>';
        if (i > 0) {
            tbody += '<input type="text" value = "marks" name="inputcell1'+j + '">';
        } else {
            tbody += '<b>' + subjectList[j] + '</b>';
        }
        tbody += '</td>';
    }
    tbody += '</tr>\n';
}
var tfooter = '</table>';
document.getElementById('wrapper').innerHTML =  theader
        + tbody + tfooter ;

and below is my attempt to read text box value:

以下是我读取文本框值的尝试:

function readTableData(){
    var marks = [];
    var table = document.getElementById("MarksTable");
    var column_count = table.rows[1].cells.length;
    var row = table.rows[1];
    if(column_count>0){
        for(var index = 0; index < column_count;index++){
            marks[index] = row.cells[index].innerHTML;
        }
    }
    return marks;
}

Here, row.cells[index].innerHTMLgives the output '<input type="text" value = "marks" name="inputcell10">.

在这里,row.cells[index].innerHTML给出输出'<input type="text" value = "marks" name="inputcell10">

回答by Khanh TO

Try this:

试试这个:

function readTableData(){
    var marks = [];
    var table = document.getElementById("MarksTable");
    var column_count = table.rows[1].cells.length;
    var row = table.rows[1];
    if(column_count>0){
        for(var index = 0; index < column_count;index++){
            marks[index] = row.cells[index].getElementsByName('inputcell' + index)[0].value;
    //Or marks[index] = document.getElementsByName('inputcell' + index)[0].value;
        }
    }
    return marks;
}

回答by Nandkishor Gokhe

<!DOCTYPE html>
<html>
<head>
<style>
table, td {
    border: 1px solid black;
}
</style>
</head>
<body>

<p>Click the button to add a new row at the first position of the table and then add cells and content.</p>
<div id="tableContainer">

</div>
<br>

<button onclick="myFunction()">Try it</button>
<button onclick="readTableData()"> Read it </button>
<script>
function myFunction() {
    var tab = '<table id="MarksTable">';
    var counter = 0;
    for(i = 0; i< 4; i++){
        tab = tab + '<tr><td rowspan = "4"> Dept1 </td><td> <input type="text" id="inputcell'+counter+'"  value="'+i+'"/> </td></tr>';
        counter++;
        tab = tab+'<tr><td> <input type="text" id="inputcell'+counter+'"  value="'+i+'"/> </td></tr>';
        counter++;
        tab = tab+'<tr><td> <input type="text" id="inputcell'+counter+'"  value="'+i+'"/> </td></tr>';
        counter++;
        tab = tab+'<tr><td> <input type="text" id="inputcell'+counter+'"  value="'+i+'"/> </td></tr>';
        counter++;
    }
    tab = tab + '</table>';
    document.getElementById("tableContainer").innerHTML = tab;
}

function readTableData(){
    var val;
    var table = document.getElementById("MarksTable");
    var column_count = table.rows[1].cells.length;
    var rowcount = table.rows.length;
    alert(rowcount);
    if(column_count>0){
        for(var index = 0; index < rowcount;index++){
            var row = table.rows[index];
            val = document.getElementById("inputcell"+index);
            alert(val.value);

            //marks = row.cells[0].getElementsByName('inputcell').value;
    //Or marks[index] = document.getElementsByName('inputcell' + index)[0].value;
        }
    }
   alert(val);
}
</script>

</body>
</html>