jquery读取子xml数据
我想知道如何修复我的脚本,我希望当您单击按钮时会出现一个警报,但它会跟上同一页面上发生的xml更改。 我原以为这个脚本可以工作,但当我点击按钮时,什么也没发生。我想看看能不能从更有经验的人那里得到一些信息jquery读取子xml数据,xml,ajax,jquery,xml-parsing,Xml,Ajax,Jquery,Xml Parsing,我想知道如何修复我的脚本,我希望当您单击按钮时会出现一个警报,但它会跟上同一页面上发生的xml更改。 我原以为这个脚本可以工作,但当我点击按钮时,什么也没发生。我想看看能不能从更有经验的人那里得到一些信息 $(document).ready(function(){ function get_info() { $.ajax({ type: "GET", url: "xmldata/product.xml", dataType: "xml",
$(document).ready(function(){
function get_info() {
$.ajax({
type: "GET",
url: "xmldata/product.xml",
dataType: "xml",
cache: false,
complete: function(doc) {
var product = $(doc.responseText).find("product").text() == 1;
var name = product.next().children("name").text();
var cost = product.next().children("cost").text();
$("#xmlalert").click(function(){
alert(cost);
})
}
});
}
setInterval(function() {
get_info();
}, 5000);
});
xml数据如图所示:
<?xml version="1.0" encoding="utf-8"?>
<store>
<product>1</product>
<info>
<name>Toy</name>
<cost>1196</cost>
</info>
</store>
1.
玩具
1196
Html代码
<button class="button" id="1">One</button>
<button class="button" id="2">Two</button>
XML是
<?xml version="1.0" encoding="utf-8"?>
<store>
<product>1</product>
<info>
<name>Toy</name>
<cost>1196</cost>
</info>
<product>2</product>
<info>
<name>Toy 2</name>
<cost>11962</cost>
</info>
</store>
1.
玩具
1196
2.
玩具2
11962
我不想问太多,但是xml文件的调用和它是否在子文件夹中有区别吗?例如,您的cml.xml也是通过使用只会找到一个的find(“产品”)来实现的吗?有没有更具体的方法?由于此xml将包含多个产品,例如产品1、产品2等。好的,我更改了对此的答案。谢谢,在查看您的答案lol之前,我实际上已经解决了这个问题,但现在它同时提醒了两个成本,因为我希望它只提醒一个。第二个按钮将是一个单独的按钮。但是如果产品数量更多,会有问题,那么可以说50-100个吗?json会更容易更好吗?
<?xml version="1.0" encoding="utf-8"?>
<store>
<product>1</product>
<info>
<name>Toy</name>
<cost>1196</cost>
</info>
<product>2</product>
<info>
<name>Toy 2</name>
<cost>11962</cost>
</info>
</store>