0

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

以下是我创建表的方式。

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 ;

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

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

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

4

2 回答 2

2

试试这个:

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;
}
于 2013-06-29T10:35:48.710 回答
0
<!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>
于 2015-06-02T11:14:53.627 回答