[英]Javascript unable to extract value of text field in Table
我有一个具有n行数的表,第8个和第9个单元格是可编辑的测试字段。 我需要使用Javascript和HTML DOM提取这些字段的值。 我尝试了不同的方法,但是每当我发出警报时,我都会收到警报内容。
以下是创建语句:
//Input Fields
for(var i=1; i<=mmrows.length-3; i++)
{//mmrows is the source
var input1 =document.createElement('input');
var input2 =document.createElement('input');
input1.setAttribute("type", "Text");
input2.setAttribute("type", "Text");
//input1.id='Inp1'.concat(i);// Tried to assign ID dynamically
//input2.id='Inp2'.concat(i);
input1.id='Inp1';
input2.id='Inp2';
input1.onkeypress=function(){
return isNumberKey(arguments[0]);
}//for validation
var t1=document.createElement("td");
t1.appendChild(input1);
valrows[i+1].cells[8].appendChild(t1);
var t2=document.createElement("td");
t2.appendChild(input2);
valrows[i+1].cells[9].appendChild(t2);
}
我试图提取另一个函数中的值,并尝试了以下方法:
var cells=document.getElementsByTagName('td');
alert(cells.length);
alert(cells[8].innerText);
alert(cells[8].innerHTML);
alert(trim(valrows[i].cells[8].innerHTML));
alert(trim(valrows[i].cells[9].innerHTML));
var str='Inp1'.concat(i);
var str1='Inp2'.concat(i);
alert(document.getElementsById(str).value); //Dynamic ID
alert(document.getElementsById(str1).value);
var tblcells = valrows[i].cells;
alert(tblcells);
for(var j=0; j<tblcells.length; j++)
{
alert(tblcells[j].firstElementChild.value);
}
请告诉我我需要进行哪些更改才能获取文本字段值。
我已经解决了! 实际上,这是我自己的一个错误; 我尝试通过说getElement(s)ById访问文本值-不需要。
因此,我可以使用循环变量i并在类似的循环中通过getElementById访问它,从而为这些文本单元动态分配“ id”。
谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.