这是我的javascript代码。
function getXmlData(){
var icode=document.getElementById("itemcodee").value;
alert(icode);
var Connect = new XMLHttpRequest();
// Define which file to open and
// send the request.
Connect.open("GET", "assets/itemdetail.xml", false);
Connect.setRequestHeader("Content-Type", "text/xml");
Connect.send(null);
// Place the response in an XML document.
var TheDocument = Connect.responseXML;
// Place the root node in an element.
var items = TheDocument.childNodes[0];
// Retrieve each item in turn.
for (var i = 0; i < items.children.length; i++)
{
var item = items.children[i];
if(item==icode)
{
// Access each of the data values.
var code = item.getElementsByTagName("code");
var name = item.getElementsByTagName("name");
var stock = item.getElementsByTagName("stock");
// print these values to corresponding field
document.getElementById("itemnamee").value=name;
document.getElementById("itemstock").value=stock;
alert(code+'|'+name+'|'+stock)
}
else
{
alert('Sorry, Item Not Found. Please check the inventory');
}
}
}
这是我的xml
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<item><code>1</code><name>Sample Item</name><stock>0</stock></item>
<item><code>3</code><name>ITEM</name><stock>205000</stock></item>
<item><code>4</code><name>ABHI UPDATED</name><stock>2201</stock></item>
<item><code>5</code><name>Item 5</name><stock>10</stock></item>
<item><code>6</code><name>item 6</name><stock>2130</stock></item>
<item><code>7</code><name>Soap</name><stock>100</stock></item>
<item><code>8</code><name>New Item</name><stock>1201</stock></item>
</xml>
当我输入数据(此处为icode)时,我希望脚本从此xml文件中读取相关数据并将其显示在相应的texbox上。 我坚持了很长时间。我尝试了各种解决方案,但仍然没有运气。
答案 0 :(得分:0)
试试这个
var item = items.children[i];
var theCode = item.getElementsByTagName("code").html;
if(theCode==icode)
{
...
} else {
...
}