我尝试使用javascript使用ajax从xml文件中打印书名,价格和日期。我花了差不多两天的时间在youtube上看了一些视频来弄明白我不能。我不知道我在哪里做错了如果你可以帮我解决你的问题。谢谢你。
$(document).ready(function() {
$("#find").click(function() {
var book = $("#book").val();
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.status == 200) {
document.getElementById('forPrice').innerHTML = 'Test1';
var i;
var xmlDoc = xhttp.responseXML;
var x = xmlDoc.getElementsByTagName("book");
for (i = 0; i < x.length; i++) {
catalog + = xmlDoc[i].getElementsByTagName("price")[0].childNodes[0].nodeValue);
document.getElementsById("#forPrice").innerHTML(catalog);
xmlDoc[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
document.getElementsById("#forBookInformation").innerHTML(catalog);
xmlDoc[i].getElementsByTagName("data")[0].childNodes[0].nodeValue);
document.getElementsById("#forPublishDate").innerHTML(catalog);
});
}
};
xhttp.open("POST", "books.xml", true);
xhttp.send();
})
})
&#13;
<?xml version="1.0"?>
<catalog>
<book>
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<price>44.95</price>
<date>2000-10-01</date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book>
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<price>5.95</price>
<date>2000-12-16</date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book>
<author>Corets, Eva</author>
<title>Oberon's Legacy</title>
<price>5.95</price>
<date>2001-03-10</date>
<description>In post-apocalypse England, the mysterious
agent known only as Oberon helps to create a new life
for the inhabitants of London. Sequel to Maeve
Ascendant.</description>
</book>
<book>
<author>Thurman, Paula</author>
<title>Splish Splash</title>
<price>4.95</price>
<date>2000-11-02</date>
<description>A deep sea diver finds true love twenty
thousand leagues beneath the sea.</description>
</book>
<book>
<author>Knorr, Stefan</author>
<title>Creepy Crawlies</title>
<price>4.95</price>
<date>2002-10-01</date>
<description>An anthology of horror stories about roaches,
centipedes, scorpions and other insects.</description>
</book>
</catalog>
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Books</title>
<style type="text/css">
table,
td {
border: 1px solid black;
}
</style>
<script src="jquery-1.11.2.js"></script>
<script src="p3.js"></script>
</head>
<body>
<h1>Books</h1>
<h2>Book Price</h2>
<form action="" method="post">
<select name="book" id="book">
<option value="XML Developer's Guide">XML Developer's Guide</option>
<option value="Midnight Rain">Midnight Rain</option>
<option value="Oberon's Legacy">Oberon's Legacy</option>
<option value="Splish Splash">Splish Splash</option>
<option value="Creepy Crawlies">Creepy Crawlies</option>
</select>
Select a book to see the price
<br>
<br>
<input type="button" id="find" value="Find!">
</form>
<div id="forPrice"></div>
<h2>Author Names and Book Titles</h2>
<div id="forBookInformation"></div>
<h2>Book Publish Date</h2>
<div id="forPublishDate"></div>
<body>
</html>
&#13;
使用javascript的ajax。我花了差不多两天的时间在youtube上看了一些视频来弄明白我不能。我不知道我在哪里做错了如果你可以帮我解决你的问题。谢谢你。
答案 0 :(得分:1)
您的代码有很多问题。让我解释一下在哪里:
xhttp.open("GET", "books.xml", true);
document.getElementsById
错误,因为只有一个ID,它应该是:document.getElementById
jQuery
语法是$('#forPrice')
但是由于您使用原生JS,因此您必须省略#
,即document.getElementById('forPrice')
- &gt;请注意缺少#
.innerHTML('text')
而是.innerHTML = 'text'
)catalog
更改为特定值(如果您愿意,可以还原此更改)。)
是不允许的。总而言之,我改变的内容并不完全正确,但至少数据已加载。但是,由于您不检查从select-option元素中选择的内容,因此数据将被覆盖。
希望这有助于您朝着正确的方向前进!
这是指向工作示例的链接。 http://plnkr.co/edit/4TrjfX4LI8234lFVwIyU?p=info
答案 1 :(得分:0)
我假设你非常了解ajax。
module Rustgem
system("cd #{File.dirname(__FILE__)}/../rust && cargo build --release")
end
var data = "<?xml version='1.0'?> <catalog> <book> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <price>44.95</price> <date>2000-10-01</date> <description>An in-depth look at creating applications with XML.</description> </book> <book> <author>Ralls, Kim</author> <title>Midnight Rain</title> <price>5.95</price> <date>2000-12-16</date> <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description> </book> <book> <author>Corets, Eva</author> <title>Oberon's Legacy</title> <price>5.95</price> <date>2001-03-10</date> <description>In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant.</description> </book> <book> <author>Thurman, Paula</author> <title>Splish Splash</title> <price>4.95</price> <date>2000-11-02</date> <description>A deep sea diver finds true love twenty thousand leagues beneath the sea.</description> </book> <book> <author>Knorr, Stefan</author> <title>Creepy Crawlies</title> <price>4.95</price> <date>2002-10-01</date> <description>An anthology of horror stories about roaches, centipedes, scorpions and other insects.</description> </book> </catalog>";
$(document).ready(function() {
var getBookByTitle = function(title){
var bookElement = undefined;
var xmlDoc = $.parseXML( data ),
xml = $(xmlDoc),
catalog = xml.find( "catalog" );
$.each(catalog.find('book'), function(i, book){
if($(book).find("title").text() == title){
bookElement = {};
bookElement.author = $(book).find("author").text();
bookElement.title = $(book).find("title").text();
bookElement.price = $(book).find("price").text();
bookElement.date = $(book).find("date").text();
}
});
return bookElement;
};
$("#find").click(function() {
var searchBook = $("#book").val();
var bookElement = getBookByTitle(searchBook);
$("#forPrice").text(bookElement.price);
$("#forBookInformation").html("<i>"+ bookElement.author +","+bookElement.title +"</i>");
$("#forPublishDate").text(bookElement.date);
});
});