简体   繁体   English

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

[英]wrong value returned while getting value of dynamically created textbox using Javascript

I've dynamically added some rows to my ASP.NET table using JavaScript, my rows contain textboxes (which are in fact spans), it is my textbox HTML which as added to my table: 我已经使用JavaScript向ASP.NET表中动态添加了一些行,我的行包含文本框(实际上是跨度),它是添加到表中的文本框HTML:

               var spanCount = document.createElement("span");
           spanCount.innerHTML = "<input style=\"width:30px\" type=\"text\" name=\"count\" value=1>";

later I change default value of this textbox (default value is 1), and I want to get current value of this dynamically created textbox using JavaScript, I use following code to get its value, but I always get 1 (default value), I want to have current value of this textbox: 稍后,我更改此文本框的默认值(默认值为1),我想使用JavaScript获取此动态创建的文本框的当前值,我使用以下代码获取其值,但我始终获取1(默认值),我希望具有此文本框的当前值:

alert(document.getElementById('<%=tblFoodList.ClientID %>').rows[1].cells[3].firstChild.innerHTML);

but I get following HTML (I can parse its value): 但我得到以下HTML(我可以解析其值):

<input style="width:30px" name="count" value="1" type="text"

as you can see, value="1", but I have changed value, is there any way that I can get current value (or HTML) of this span? 如您所见,value =“ 1”,但是我更改了value,有什么方法可以获取此范围的当前值(或HTML)吗?

You should use .value instead of .innerHTML to get the text value of an input. 您应该使用.value而不是.innerHTML来获取输入的文本值。

Assuming your structure: 假设您的结构:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM