简体   繁体   English

JavaScript:如何访问表第二行中的文本框

[英]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: 包含:

  • Giving each element an id and getting it using document.getElementById 给每个元素一个id并使用document.getElementById获取它
  • Using 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. 这不是首选方法,因为可以有多个具有相同名称的元素,但是只能有一个具有相同ID的元素。
  • Using the form directly. 直接使用表格。 For example if your form's name is form1: form1.subAcc 例如,如果您的表单名称为form1: form1.subAcc
  • Using 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.

相关问题 如何使用javascript访问html表中每行的第二个单元格? - How to access second cell of each row in a html table using javascript? 如何使用 Javascript 获取表格行中的第二个复选框值 - How to Get the Second Checkbox Value in a Table Row Using Javascript 如何使用 Javascript、表格值复选框、文本框、单选框、选择选项在表格中添加行? - How to add row in table with Javascript , table value checkbox,textbox,radio,select option? 如何使用javascript访问表中特定行的控件? - How to access controls of a specific row in a table using javascript? 我如何在javascript中访问表中的动态行值 - How do i access dynamic row values in table in javascript 如何在javascript中访问单击表行的特定单元格 - How to access specific cell of clicked table row in javascript 如何使用javascript或jquery获取在表的特定行中存在的复选框和文本框值 - how to get checkbox and textbox value present in a particular row of a table using javascript or jquery 如何使用文本框动态添加表格行? - How to dynamically add a table row with textbox? 如何判断一行是否包含带有JavaScript文本框的单元格 - How to tell if a row contains cells with textbox with javascript JavaScript:如何使用第一个表的节点访问div中的第二个表 - JavaScript: How to access the second table inside a div using the node of the first table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM