【问题标题】:Check XML node exists by Value using JQuery使用 JQuery 按值检查 XML 节点是否存在
【发布时间】:2010-11-29 00:21:54
【问题描述】:

我正在尝试使用 Jquery 检查 xml 节点中的值是否存在。 xml字符串是:

        <SectionAnswers>
            <SectionAnswer>
                <detailID>2216</detailID>
                <SourceAnswerID>979</SourceAnswerID>
            </SectionAnswer>
            <SectionAnswer>
                <detailID>2218</detailID>
                <SourceAnswerID>981</SourceAnswerID>
            </SectionAnswer>
            <SectionAnswer>
                <detailID>2219</detailID>
                <SourceAnswerID>977</SourceAnswerID>
            </SectionAnswer>
            <SectionAnswer>
                <detailID>2221</detailID>
                <SourceAnswerID>980</SourceAnswerID>
            </SectionAnswer>
            <SectionAnswer>
                <detailID>2282</detailID>
                <SourceAnswerID>64</SourceAnswerID>
            </SectionAnswer>
            <SectionAnswer>
                <detailID>2283</detailID>
                <SourceAnswerID>978</SourceAnswerID>
            </SectionAnswer>
            <SectionAnswer>
                <detailID>2596</detailID>
                <SourceAnswerID>73</SourceAnswerID>
            </SectionAnswer>
        </SectionAnswers>

当我尝试使用以下方法查询它的值时:

$("SectionAnswers", Section).find("64") //Section是jquery上下文

我收到以下回复:

表达式不返回 DOM 节点。

.//-->64

有什么想法我会出错吗?我真的不想像在 $("SectionAnswers", Section).each() 中那样每次循环检查值

谢谢

【问题讨论】:

    标签: jquery xml search


    【解决方案1】:

    尝试使用简单的 $.each 遍历 XML:

    $('SectionAnswers > SectionAnswer').each(function() {
        if($(this).find('SourceAnswerID').text() == '64') {
            alert('Found 64 at detailID: ' + $(this).find('detailID').text());
        }
    });
    

    或使用filter:

    var $sa = $('SectionAnswers > SectionAnswer').filter(function() {
        return $(this).find('SourceAnswerID').text() == '64'; 
    });
    alert($sa.find('SourceAnswerID').text());
    alert($sa.find('detailID').text());
    

    【讨论】:

    • 如果你想得到一个只包含所需元素的 jQuery 对象,你也可以使用过滤器。
    【解决方案2】:

    我把它留在这里作为参考,因为它可能在稍微不同的情况下有用,但是,就像提到的 karim79 一样,它匹配任何有 64 作为子字符串的东西。


    你应该可以使用":contains(text)" pseudo-selector:

    $("SectionAnswers SourceAnswerID:contains('64')", Section)
    

    这将选择 SourceAnswerID 元素,因此您可能需要使用 parent()closest() 函数来向上移动层次结构。

    【讨论】:

    • 问题在于,如果匹配 164、3064 等,则必须进一步过滤,所以问题是是否使用 contains() 并进一步过滤结果或循环遍历每个节点,直到该值匹配。
    【解决方案3】:

    谢谢大家。

    我会玩这两个,看看我能想出什么。如果可能的话,我想避免循环,因为它已经在循环内运行,而我只有一个关于嵌套循环的按钮......

    但现在我想起来了......无论如何,过滤器和包含调用都是内部循环......所以它实际上可能表现得更好,它只是简单地获取集合并遍历它。

    【讨论】:

      【解决方案4】:

      好的,解决了。我最终更改了 xml 以制作 ID 属性,所以它是

              <SectionAnswers>
                  <SectionAnswer SourceAnswerID="1487"
                                 detailID="1420" />
                  <SectionAnswer SourceAnswerID="1488"
                                 detailID="2039" />
              </SectionAnswers>
      

      我现在可以通过

      找到它

      find("SectionAnswer[SourceAnswerID=1487]")

      无论如何这是一个更好的解决方案,因为将信息放在属性中会减少返回值的大小。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-29
        • 1970-01-01
        • 2023-04-03
        相关资源
        最近更新 更多