Javascript 如何获取节点的id名称?

Javascript 如何获取节点的id名称?,javascript,json,dom,Javascript,Json,Dom,我有一个div标签,如下所示: <div id="DataEntryForm" style="position:absolute; left:100px;top:175px;width:350px; z-index:2;background-color:yellow; visibility:hidden;border-top-color: black;"> <table id="DataEntryFormTable" style=" width:100%" style

我有一个
div
标签,如下所示:

<div id="DataEntryForm" style="position:absolute; left:100px;top:175px;width:350px;  z-index:2;background-color:yellow; visibility:hidden;border-top-color: black;">
    <table id="DataEntryFormTable" style=" width:100%" style="margin-top: -50px;">
        <tr><td>&nbsp;Build Name</td><td> <input type="text" id="BuildName" name="BuildName" value="" /></td></tr>
        <tr><td>&nbsp;Build Description</td><td> <input type="text" id="BuildDesc" name="BuildDesc" value="" /></td></tr>
        <tr><td>&nbsp;Software Details</td><td> <input type="text" id="SoftwareDetail" name="SoftwareDetail" value="" /> </td></tr>
        <tr><td>&nbsp;Hardware Details</td><td> <input type="text" id="HardwareDetail" name="HardwareDetail" value="" /> </td></tr><br>
        <tr><td>&nbsp;</td></tr>
        <tr>
            <td>
                <input type="button" value="Save" onclick="saveRecord()" /></td>
            <td> <input type="button" value="Cancel"  onclick="cancelOperation()"/></td>
        </tr>
        <br>
    </table>
</div>

构建名称
构建描述
软件详细信息
硬件详细信息

我想将值从数据库加载到输入类型文本。因此,我需要获取每个节点的id,以便与我的json值和节点id匹配,并将值分配给它。我正试图用以下方法来实现这一点。但是,我无法获取id值。任何人,请帮我解决这个问题。谢谢

var nodes = deform.childNodes;
index =0;

// Load the first record in the collection.
// It is expected to have only one object in JS Object collection
var dbRecord= dbRecords[0];

while (index < nodes.length)
{
    if(nodes[index].type=="text")
    {
        nodes[index].value = dbRecord[nodes[index].id];
    }

    index++;
}
var nodes=deform.childNodes;
指数=0;
//加载集合中的第一条记录。
//JS对象集合中应该只有一个对象
var dbRecord=dbRecords[0];
while(索引<节点长度)
{
if(节点[索引]。类型==“文本”)
{
节点[index].value=dbRecord[nodes[index].id];
}
索引++;
}

id
是正确的属性。问题在于,您没有从表中检索到要检索的节点。使用.childrends()或.childNodes(),只能获取元素的直接子元素,因此您需要进一步深入表以访问您试图填充的文本输入。或者,如果要使用jQuery,可以使用单个选择器:

$(“#DataEntryFormTable输入[type='text'])”

如果不使用jQuery,我会递归地使用.children()来查找要查找的元素

编辑:另外,请确保在调用函数时包含括号,以便
var nodes=deform.childNodes
会是
var nodes=deform.childNodes()

再次编辑: ... 我没有看到你提供的数据。由于您在JSON数据中具有所需的ID,因此可以使用这些ID直接查找元素。试试这个:

dbRecord= [{"HardwareDetail":"[B","BuildDesc":"Testing1","BuildID":"BL002","BuildName":"Se­cond Name","SoftwareDetail":"ss"}];
record = dbRecord[0];
for (attr in record){
    el = document.getElementById(attr);
    if (el)
        document.getElementById(attr).value = record[attr];
    else
        console.debug('no HTML element with ID ' + attr);
}

我认为,
console.debug
在某些浏览器(IE?)中不起作用,所以当您完成测试时,您会希望将该部分去掉。

您能给我们展示一个实例,或者至少是
dbRecord
的内容吗?这是dbRecord内容--[{“硬件细节”:“[B”,“BuildDesc”:“Testing1”,“BuildID”:“BL002”,“BuildName”:“Second Name”,“SoftwareDetail”:“ss”}]您可能希望使用for循环,或者说
while(index++
此循环是无限的。我无法执行这些代码。如何打破此循环。为什么它是无限的?它不应该是无限的,但您的dbRecord在数组中包含一个对象,我编写代码时就好像数组不在那里,或者您发布的数据是dbRecords,而不是dbRecord。您可能需要访问数组b的第一个元素在迭代属性之前。我将更新代码以澄清。好的。谢谢。现在我得到了它。原因是节点索引循环仍然存在。这就是循环无限的原因。现在它正在工作。