使用Javascript获取动态创建的文本框的值时返回错误的值

使用Javascript获取动态创建的文本框的值时返回错误的值,javascript,html,Javascript,Html,我已经使用JavaScript动态地将一些行添加到我的ASP.NET表中,我的行包含文本框(实际上是跨度),添加到我的表中的是我的文本框HTML: var spanCount = document.createElement("span"); spanCount.innerHTML = "<input style=\"width:30px\" type=\"text\" name=\"count\" value=1>"; var

我已经使用JavaScript动态地将一些行添加到我的ASP.NET表中,我的行包含文本框(实际上是跨度),添加到我的表中的是我的文本框HTML:

               var spanCount = document.createElement("span");
           spanCount.innerHTML = "<input style=\"width:30px\" type=\"text\" name=\"count\" value=1>";
var spanCount=document.createElement(“span”);
spanCount.innerHTML=“”;
后来我更改了这个文本框的默认值(默认值为1),我想用JavaScript获取这个动态创建的文本框的当前值,我使用下面的代码获取它的值,但我总是得到1(默认值),我想获取这个文本框的当前值:

alert(document.getElementById('<%=tblFoodList.ClientID %>').rows[1].cells[3].firstChild.innerHTML);
alert(document.getElementById(“”).rows[1]。cells[3]。firstChild.innerHTML);
但我得到了以下HTML(我可以解析它的值):


您应该使用
.value
而不是
.innerHTML
来获取输入的文本值

假设您的结构:

alert(document.getElementById('<%=tblFoodList.ClientID%>') // this is your table
  .rows[1] // second row
  .cells[3] // third column
  .firstChild // the span you created
  .firstChild // the input
  .value // the value of the input
);
alert(document.getElementById(“”)//这是您的表
.rows[1]//第二行
.cells[3]//第三列
.firstChild//您创建的跨度
.firstChild//输入
.value//输入的值
);

更改后,您在哪里检查HTML?您使用的浏览器是什么?您在哪里为textbox指定了id“tblFoodList”?我使用firefox,tblFoodList是我的ASP.NET表的名称,我的textbox(span)在我的表的一个单元格中。当我使用textContent而不是innerHTMLuse值而不是textContent时,我什么也得不到。另外,请注意两个
第一个孩子
s,因为您需要的是输入的值,而不是跨度。谢谢Saml,您的答案非常有效!现在我得到了当前值,也不需要解析HTML!谢谢
alert(document.getElementById('<%=tblFoodList.ClientID%>') // this is your table
  .rows[1] // second row
  .cells[3] // third column
  .firstChild // the span you created
  .firstChild // the input
  .value // the value of the input
);