检索位于表单元格< td>中的表单字段隐藏值. [英] Retrieving a form field hidden value thats located in table cell <td>
问题描述
我有一个表,其中的单元格包含隐藏的输入类型.您如何访问此隐藏输入类型的值?
I have a table with a cell that contains a hidden input type. How do you go about accessing the value of this hidden input type?
HTML表格:
<table id = "tableId">
<tr>
<td>FirstName LastName</td>
<td><input type="hidden" value="userId" /><td>
</tr>
JavaScript
Javascript
var table= document.getElementById("tableId");
var tblTR = table.getElementsByTagName("tr");
for (var i = 0; i<tblTR.length; i ++) {
var tr = tblTR[i];
var tds = tr.getElementsByTagName("td");
console.debug(tds[0]); //gets the whole cell
console.debug(tds[1].innerHTML); //gets content of cell, but not the actual value of the hidden input
}
使用第二个console.debug语句,我要查找的值是"userId"而不是
With the second console.debug statement, the value I'm looking for is "userId" and not
<input type="hidden" value="userId" />
据我了解, td元素不能具有值属性,因此我不能只是做一个 tds [i] .value; .
From what I understand, a td element cannot have a value attribute, therefore I can't simply just do a tds[i].value;.
或者,还有另一种方法可以用来存储此隐藏值吗?
Alternatively, is there another approach that can be used to store this hidden value?
推荐答案
以下作品:
var inputs = document.getElementsByTagName('input');
for (i=0; i<inputs.length; i++){
if (inputs[i].type == 'hidden'){
alert(inputs[i].value);
}
}
我建议将 inputs [i] .value
分配给变量或数组,尽管不要简单地 alert()
-:
I'd suggest assigning the inputs[i].value
to a variable, or an array, though rather than simply alert()
-ing it:
var inputs = document.getElementsByTagName('input');
var values;
for (i=0; i<inputs.length; i++){
if (inputs[i].type == 'hidden'){
values = inputs[i].value;
}
}
这篇关于检索位于表单元格< td>中的表单字段隐藏值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!