如何使用javascript获取警报消息表中的textbox值

如何使用javascript获取警报消息表中的textbox值,javascript,html,Javascript,Html,这是使用javascript动态创建的表,我想使用GetCellValues()函数在警报消息中显示此文本框值 function makeTable() { row=new Array(); cell=new Array(); row_num=20; cell_num=4; tab=document.createElement('table'); tab.setAttribute('id','newtable');

这是使用javascript动态创建的表,我想使用
GetCellValues()
函数在警报消息中显示此文本框值

function makeTable()
{
    row=new Array();    
    cell=new Array();

    row_num=20; 
    cell_num=4;

    tab=document.createElement('table');    
    tab.setAttribute('id','newtable');

    tbo=document.createElement('tbody');    
    tbo.setAttribute('id','tabody');

    for(c = 0;c < row_num; c++)   
    {
        row[c]=document.createElement('tr');

        for(k = 0; k < cell_num; k++)
        {
            cell[k] = document.createElement('td');
            if (k > 0)
            {
                cont=document.createElement("input");
                cont.setAttribute('type','text');
                cell[k].appendChild(cont);
                row[c].appendChild(cell[k]);
            }            
            else
            {
                cont=document.createTextNode("0" + (c+1));
                cell[k].appendChild(cont);
                row[c].appendChild(cell[k]);                    
            }
       }

       tbo.appendChild(row[c]);
    }  

    tab.appendChild(tbo);
    document.getElementById('mytable').appendChild(tab);
    mytable.setAttribute("align", "top-left");
}

如果您收到一个包含
[object HTMLCollection]
消息的警报框,那么您需要使用
alert(cont[0].value)
代替
alert(cont.value)
函数末尾的
GetCellValues
返回集合,需要遍历它或假设第一个元素-应用于
单元格
,例如

function makeTable()
{
    row=new Array();    
    cell=new Array();

    row_num=20; 
    cell_num=4;

    tab=document.createElement('table');    
    tab.setAttribute('id','newtable');

    tbo=document.createElement('tbody');    
    tbo.setAttribute('id','tabody');

    for(c = 0;c < row_num; c++)   
    {
        row[c]=document.createElement('tr');

        for(k = 0; k < cell_num; k++)
        {
            cell[k] = document.createElement('td');
            if (k > 0)
            {
                cont=document.createElement("input");
                cont.setAttribute('type','text');
                cell[k].appendChild(cont);
                row[c].appendChild(cell[k]);
            }            
            else
            {
                cont=document.createTextNode("0" + (c+1));
                cell[k].appendChild(cont);
                row[c].appendChild(cell[k]);                    
            }
       }

       tbo.appendChild(row[c]);
    }  

    tab.appendChild(tbo);
    document.getElementById('mytable').appendChild(tab);
    mytable.setAttribute("align", "top-left");
}
var rows = tbo.getElementsByTagName('tr');
for (var c = 0; c < row_num; c++) {
     row = rows[c];
     var cells = row.getElementsByTagName('td');
     for (var k = 0; k < cell_num; k++) {
          cell = cells[k];
          // and so on
     }
}
var rows=tbo.getElementsByTagName('tr');
对于(var c=0;c

函数makeTable(){
行=新数组();
cell=新数组();
行数=20;
单元数=4;
tab=document.createElement('table');
tab.setAttribute('id','newtable');
tbo=document.createElement('tbody');
tbo.setAttribute('id','tabody');
对于(c=0;c0){
cont=document.createElement(“输入”);
cont.setAttribute('type','text');
cont.setAttribute('值','');
单元格[k]。追加子项(续);
第[c]行。追加子(单元格[k]);
}
否则{
cont=document.createTextNode(“0”+(c+1));
单元格[k]。追加子项(续);
第[c]行。追加子(单元格[k]);
}
}
tbo.appendChild(第[c]行);
}
表2.附加子项(tbo);
document.getElementById('mytable').appendChild(制表符);
setAttribute(“对齐”,“左上”);
}
makeTable();
函数GetCellValues(行数、单元格数){
var arrinport=document.getElementsByTagName('input');
var指数=(行数-1)*(单元格数-1);
警报(输入[index].value);
}        

我认为您需要对GetCellvalues函数进行一些修改,如
tab.getElementsByTagName('tbody')
将不会获取标记名为tbody的元素。您应该使用
document.getElementsByTagName


您可以检查代码的工作演示

快捷方式是使用标记的ID属性

ID为的TD标签示例:

<td id="1_1">1st row 1st column</td><td id="1_2">1st row 2nd column</td>
1行第1列第1行第2列
获取ID为的TD的Javascript:

var row_len=1,col_len=2;

for (r = 0; r< row_len; r++) {
    for (c = 0; c < coll_len; c++) {
        alert(document.getElementById(r+'_'+c));        
    }
}
var行长度=1,列长度=2;
对于(r=0;r
警报(cont[0].value)
如果您需要用于循环输入的所有单元格的值
var row_len=1,col_len=2;

for (r = 0; r< row_len; r++) {
    for (c = 0; c < coll_len; c++) {
        alert(document.getElementById(r+'_'+c));        
    }
}