代码之家  ›  专栏  ›  技术社区  ›  Munzilla

使用Jquery以checkbox格式过滤XML

  •  1
  • Munzilla  · 技术社区  · 16 年前

    我正在通过AJAX(jquery)引入一些XML,我希望能够使用复选框等进行过滤。类似于这样的网站: http://www.worldmarket.com/giftfinder/index.jsp

      <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()中添加一些东西,上面写着fine元素,其中tags=(我知道它正漂向陆地,但是……) 无论如何,任何帮助或指导都将不胜感激。 提前谢谢!

    再次感谢你的帮助。

    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);
    

    抱歉,我想我只是没有得到什么。 谢谢。

    1 回复  |  直到 16 年前
        1
  •  1
  •   Andir    16 年前

    例如,如果您希望获得“语言”为“英语”的所有元素,您应该能够使用:

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

    你可以把过滤器链起来,得到你需要的东西。上面返回所有key=“Language”和value包含English的元素节点。 如果需要获取英语语言和政治标记,可以添加另一个筛选器:

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