2010-07-09 74 views
1

stackoverflow newbie here,所以我希望你能通过帮助我解决问题给我一个温暖的welcoe。我发现了几个与此类似的问题和答案,但没有一个正是我所需要的。 我通过AJAX(jQuery的),我想能够拉动一些XML使用复选框等来筛选类似这样的网站: http://www.worldmarket.com/giftfinder/index.jsp通过jquery以复选框格式过滤XML

XML是不是最好的方式格式化(我有无法控制),以及类似于这样对我拉中的每个元素:

<element text="something.exe" value="dev|something.exe" depth="0" lastUpdate="2010-03-09 09:50" nextUpdate="" category="Entertainment" filesize="2 MB"> 
    <attribute key="Description" value="here is the description"></attribute> 
    <attribute key="Language" value="English,Russian,Ukrainian"></attribute> 
    <attribute key="Function" value="History, Education"></attribute> 
    <attribute key="Industry" value="Public Sector,Sports"></attribute> 
    <attribute key="Title" value="Something"></attribute> 
    <attribute key="Tags" value="Politics"></attribute> 
    </element> 

所以我没有问题,只需通过循环和使用这样的代码显示在网格中的所有元素:

$(this).find("element").each(function(index){ 
var appTitle = $(this).find("attribute[key='Title']").attr("value") || $(this).attr("text"); 

但我遇到的问题是过滤标签。我想在左边有几个复选框,这些复选框与XML中的不同标签(或行业或功能属性)相关联,并且在单击时使结果集过滤器仅显示具有这些标签的元素。 这样做的最好方法是什么?这就好像我想向.find()添加一些东西,这些东西说的是精细的元素WHERE tags = ...(我知道这是漂流到SQL的土地上,但是...) 无论如何,任何帮助或方向将大受赞赏。 在此先感谢!

再次感谢您的帮助。
我试过使用.has,到目前为止还不能真正使它工作。哪怕只是用我上面列出的虚拟数据测试它在一般情况下,这是行不通的:

var testThisData = '<element text="something.exe" value="dev|something.exe" depth="0" lastUpdate="2010-03-09 09:50" nextUpdate="" category="Entertainment" filesize="2 MB"><attribute key="Description" value="here is the description"></attribute><attribute key="Language" value="English,Russian,Ukrainian"></attribute><attribute key="Function" value="History, Education"></attribute><attribute key="Industry" value="Public Sector,Sports"></attribute><attribute key="Title" value="Something"></attribute><attribute key="Tags" value="Politics"></attribute></element>'; 
console.log($(testThisData).has("attribute[key='Language'][value~='English']").length);` 

返回0,因为即使做到这一点:

console.log($(testThisData).has("attribute").length); 

对不起,我想我只是不得到的东西。 谢谢。

+0

不要忘记,以纪念我的答案,如果它帮助你。 – Andir 2010-07-13 02:27:04

回答

1

如果你正在寻找让所有用“英语”的“语言”的元素,例如,你应该能够使用:

$("element").has("attribute[key='Language'][value~='English']"); 

您可以链过滤器来得到你所需要的。以上返回所有具有key =“Language”和包含英语的值的元素节点。 如果您需要获得英语和政治标签,你可以添加其他过滤器:

$("element").has("attribute[key='Language'][value~='English']").has("attribute[key='Tags'][value~='Politics']") 
+0

感谢您的评论。我认为让我更困惑的是如何在有多个过滤器时对数据进行排序。因此,您在上面发布的代码应该会为我提供属性中包含英语的所有元素,但将英语作为语言和政治作为标记的所有元素又是如何呢? 如果我第一次加载时将XML转换为JSON,会不会最好? 谢谢。 – Munzilla 2010-07-09 17:57:39

+0

编辑过的文章,以进一步扩大你想要的过滤器。 – Andir 2010-07-09 23:40:34

+0

谢谢,我编辑了我的原文,解释我遇到的一些困难。 – Munzilla 2010-07-19 15:58:59