[英]JavaScript: how to access the textbox in the second row of the table
I have an html code like this - 我有这样的html代码-
<table border="0" cellspacing="2" cellpadding="2" width="99%" id="subAccTable">
<tr>
<h2>Sub Accounts</h2>
</tr>
<tr>
<th>action </th>
<th>account</th>
<th>homeDir</th>
<th>primaryGroup</th>
</tr>
<tr>
<td><input type="hidden" name="vtierId" value="" /></td>
<td><input type="text" name="subAcc"
value=""/></td>
<td><input type="text" name="subHomeDir"
value=""/></td>
<td><input type="text" name="subPriGroup"
value=""/></td>
</tr>
</table>
Now i want to fill the values of textboxes named subAcc, subHomeDir, subPriGroup using javascript. 现在,我想使用JavaScript填充名为subAcc,subHomeDir,subPriGroup的文本框的值。 How can i do it ?
我该怎么做 ?
There are multiple ways to get the proper DOMElement; 有多种方法可以获取正确的DOMElement。 including:
包含:
document.getElementById
document.getElementById
获取它 document.getElementsByName
. document.getElementsByName
。 This is not preferred since there can be multiple elements with the same name, however there can be only one with the same id. form1.subAcc
form1.subAcc
document.getElementsByTagName('input')
and then getting the proper index. document.getElementsByTagName('input')
,然后获取正确的索引。 I'd recommend using the id to retrieve the proper element. 我建议使用id检索适当的元素。
The easiest way would be to give these textboxes a unique id, then reference them like this: 最简单的方法是为这些文本框赋予唯一的ID,然后像这样引用它们:
<input type="text" id="subHomeDir" name="subHomeDir" value=""/>
var tb = document.getElementById("subHomeDir");
tb.value = "foo";
If you're stuck with the names only, then you can use document.getElementsByName
, just remember, this will return a collection of elements (since names are not necessarily unique), which you'll have to index: 如果仅使用名称,则可以使用
document.getElementsByName
,请记住,这将返回元素的集合 (因为名称不一定是唯一的),您必须对其进行索引:
var tb = document.getElementsByName("subHomeDir")[0];
tb.value = "foo";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.