[英]Getting text values from dynamic HTML table
I am trying to get the values from an html table.我正在尝试从 html 表中获取值。 I have tried
row.cells[j].innerHTML
which returns <input name="ctl00$ctl00$ctl00$MainContent$Nav$SiteContent$Text2" type="text" id="MainContent_Nav_SiteContent_Text2" maxlength="5">
我试过
row.cells[j].innerHTML
返回<input name="ctl00$ctl00$ctl00$MainContent$Nav$SiteContent$Text2" type="text" id="MainContent_Nav_SiteContent_Text2" maxlength="5">
I have also tried row.cells[j].innerHTML.text
, row.cells[j].innerHTML.value
, and row.cells[j].outterHTML
which all return undefined
.我还尝试
row.cells[j].innerHTML.text
、 row.cells[j].innerHTML.value
和row.cells[j].outterHTML
,它们都返回undefined
。 Any ideas?有任何想法吗?
An overview of what I want happening: user enter values in the dynamic table, adding / deleting rows as needed.我想要发生的事情的概述:用户在动态表中输入值,根据需要添加/删除行。 Once table is filled, user clicks save which calls
GetTableValues()
which loops through the table adding each fields value to a string.表格填满后,用户单击保存,调用
GetTableValues()
循环遍历表格,将每个字段值添加到字符串中。 Each value is separated by % and each row is separated by @.每个值由 % 分隔,每行由 @ 分隔。 I then assign that string to a hidden field which I can access in my VB.Net code which then parses the data to save to a database.
然后我将该字符串分配给一个隐藏字段,我可以在我的 VB.Net 代码中访问该字段,然后解析数据以保存到数据库。
It is looping through table but (as seen in the logs below), it does not get the values from the table它正在遍历表但是(如下面的日志所示),它没有从表中获取值
Here is the javascript and html of the table and looping through the table.这里是表格的javascript和html,循环表格。
function GetTableValues() {
var s = "";
console.log("enter function");
//Reference the Table.
var table = document.getElementById("dataTable");
//Loop through Table Rows.
for (var i = 1; i < table.rows.length; i++) {
//Reference the Table Row.
var row = table.rows[i];
console.log("outside nest " + s);
for (var j = 1; j < 6; j++) {
console.log("i= " + i + " j= " + j);
//Copy values from Table Cell to JSON object.
console.log("inside nest " + row.cells[j].innerHTML +"%");
s = s + row.cells[j].innerHTML +"%";
}
console.log("outside again " + s);
s = s + "@";
}
document.getElementsByName("drawingsHidden").value = s
console.log(document.getElementsByName("drawingsHidden").value);
}
<table id="dataTable" style="width:100%">
<tr>
<th>Check Box</th>
<th>CAGE</th>
<th>Dwg #</th>
<th>Dwg Rev</th>
<th>Prop Rev</th>
<th>Issued Rev</th>
<th>Status</th>
</tr>
<tr>
<td><input type="checkbox" id="chkbox" /></td>
<td><input type="text" id="Text2" maxlength="5" runat="server" text=""/></td>
<td><input type="text" id="DRAWINGNUM" maxlength="20" runat="server" text=""/></td>
<td><input type="text" id="DRAWINGREV" maxlength="2" runat="server" text=""/></td>
<td><input type="text" id="PROPREV" maxlength="2" runat="server" text=""/></td>
<!--tie these fields to the drawing tracking form-->
<td><input type="text" id="ISSUEDREV" maxlength="2" runat="server" text=""/></td>
<td><input type="text" id="Text3" maxlength="20" runat="server" text=""></td>
</tr>
</table>
enter function
outside nest
i= 1 j= 1
inside nest <input name="ctl00$ctl00$ctl00$MainContent$Nav$SiteContent$Text2" type="text" id="MainContent_Nav_SiteContent_Text2" maxlength="5" text="">%
i= 1 j= 2
inside nest <input name="ctl00$ctl00$ctl00$MainContent$Nav$SiteContent$DRAWINGNUM" type="text" id="MainContent_Nav_SiteContent_DRAWINGNUM" maxlength="20" text="">%
i= 1 j= 3
inside nest <input name="ctl00$ctl00$ctl00$MainContent$Nav$SiteContent$DRAWINGREV" type="text" id="MainContent_Nav_SiteContent_DRAWINGREV" maxlength="2" text="">%
i= 1 j= 4
inside nest <input name="ctl00$ctl00$ctl00$MainContent$Nav$SiteContent$PROPREV" type="text" id="MainContent_Nav_SiteContent_PROPREV" maxlength="2" text="">%
i= 1 j= 5
inside nest <input name="ctl00$ctl00$ctl00$MainContent$Nav$SiteContent$ISSUEDREV" type="text" id="MainContent_Nav_SiteContent_ISSUEDREV" maxlength="2" text="">%
outside again <input name="ctl00$ctl00$ctl00$MainContent$Nav$SiteContent$Text2" type="text" id="MainContent_Nav_SiteContent_Text2" maxlength="5" text="">%<input name="ctl00$ctl00$ctl00$MainContent$Nav$SiteContent$DRAWINGNUM" type="text" id="MainContent_Nav_SiteContent_DRAWINGNUM" maxlength="20" text="">%<input name="ctl00$ctl00$ctl00$MainContent$Nav$SiteContent$DRAWINGREV" type="text" id="MainContent_Nav_SiteContent_DRAWINGREV" maxlength="2" text="">%<input name="ctl00$ctl00$ctl00$MainContent$Nav$SiteContent$PROPREV" type="text" id="MainContent_Nav_SiteContent_PROPREV" maxlength="2" text="">%<input name="ctl00$ctl00$ctl00$MainContent$Nav$SiteContent$ISSUEDREV" type="text" id="MainContent_Nav_SiteContent_ISSUEDREV" maxlength="2" text="">%
row.cells[j]
is a TD Element , not an Input element. row.cells[j]
是一个TD Element ,而不是一个 Input 元素。
By doing console.log(row.cells[j])
it's the easiest way to detect what is actually hold by some property.通过执行
console.log(row.cells[j])
,这是检测某些属性实际持有的内容的最简单方法。 Then, having that element all it takes is to query for a child element Input.然后,拥有该元素所需要做的就是查询子元素 Input。
const EL_input = row.cells[j].querySelector("input")
. const EL_input = row.cells[j].querySelector("input")
。 Now that you have your input Element: const value = EL_input.value
现在您有了输入元素:
const value = EL_input.value
.console.log()
or debugger
to test your code..console.log()
或debugger
来测试您的代码。 // DOM helpers: const EL = (sel, par) => (par || document).querySelector(sel); const ELS = (sel, par) => (par || document).querySelectorAll(sel); // Task: const getTableValues = () => { let str = ""; ELS("#dataTable tbody tr").forEach(EL_tr => { ELS("td", EL_tr).forEach(EL_td => { str += EL("input", EL_td).value + "%"; }); str += "@"; }); EL("#drawingsHidden").value = str }; EL("#test").addEventListener("click", getTableValues);
#dataTable { width: 100%; }
<table id=dataTable> <thead> <tr> <th>Check Box</th> <th>CAGE</th> <th>Dwg #</th> <th>Dwg Rev</th> <th>Prop Rev</th> <th>Issued Rev</th> <th>Status</th> </tr> </thead> <tbody> <tr> <td><input type=checkbox></td> <td><input type=text maxlength=5></td> <td><input type=text maxlength=20></td> <td><input type=text maxlength=2></td> <td><input type=text maxlength=2></td> <td><input type=text maxlength=2></td> <td><input type=text maxlength=20></td> </tr> </tbody> </table> <button id=test type=button>CLICK TO TEST</button><br> <input id=drawingsHidden type=text>
var table = document.getElementsByTagName('table')[0] var td = table.getElementsByTagName('td')[0] var input = td.getElementsByTagName('input')[0] console.log(input.value)
<table> <tr> <td><input value=7><td> </tr> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.