如何获取Node的id名称?
我有一个 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> Build Name</td><td> <input type="text" id="BuildName" name="BuildName" value="" /></td></tr>
<tr><td> Build Description</td><td> <input type="text" id="BuildDesc" name="BuildDesc" value="" /></td></tr>
<tr><td> Software Details</td><td> <input type="text" id="SoftwareDetail" name="SoftwareDetail" value="" /> </td></tr>
<tr><td> Hardware Details</td><td> <input type="text" id="HardwareDetail" name="HardwareDetail" value="" /> </td></tr><br>
<tr><td> </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++;
}
I have a div
tag as follows:
<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> Build Name</td><td> <input type="text" id="BuildName" name="BuildName" value="" /></td></tr>
<tr><td> Build Description</td><td> <input type="text" id="BuildDesc" name="BuildDesc" value="" /></td></tr>
<tr><td> Software Details</td><td> <input type="text" id="SoftwareDetail" name="SoftwareDetail" value="" /> </td></tr>
<tr><td> Hardware Details</td><td> <input type="text" id="HardwareDetail" name="HardwareDetail" value="" /> </td></tr><br>
<tr><td> </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>
I want to load the values from the database to the input type text. So, I need to get the id of each node in order to match with my json value and node id and to assign the value to it. I'm trying with the following to achieve this. But, I cannot get the id value. Anyone, please help me out on this. Thanks ..
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++;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
id
是正确的属性。问题是您没有检索您尝试从表中检索的节点。使用 .children() 或 .childNodes() 只能获取元素的直接子元素,因此您需要进一步深入到表中以访问您尝试填充的文本输入。或者,如果您想使用 jQuery,单个选择器就可以解决问题:$("#DataEntryFormTable input[type='text']")
如果您不使用 jQuery,我会使用.children() 递归地查找您要查找的元素。
编辑:另外,请确保在调用函数时包含括号,因此
var 节点 = deform.childNodes;
会是
:
再次编辑
...我没有看到你提供的数据。由于 JSON 数据中有所需的 ID,因此您可以直接使用这些 id 查找元素。试试这个:
我认为
console.debug
在某些浏览器(IE?)中不起作用,因此当您完成测试时,您需要将该部分删除。id
is the correct property. the problem is that you are not retrieving the nodes that you are trying to retrieve from your table. Using .children() or .childNodes(), only gets direct children of your element, so you need to drill down further into your table to access the text inputs your are trying to fill. Alternatively, if you want to use jQuery, a single selector could do the trick:$("#DataEntryFormTable input[type='text']")
If you don't use jQuery, I would use .children() recursively to find the elements you're looking for.
edit: Also, make sure you include parenthesis when calling a function, so
var nodes = deform.childNodes;
would be
var nodes = deform.childNodes();
edit again:
... I didn't see the data that you provided. since you have the ID's that you need in your JSON data, you can look up the elements directly using those ids. Try this:
I don't think
console.debug
works in some browsers (IE?), so you'll want to take that part out when you're finished testing.