jquery - 使用 JQuery 按值检查 XML 节点是否存在

标签 jquery xml search

我正在尝试使用 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() 那样每次都循环检查值

谢谢

最佳答案

尝试使用简单的 $.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 - 使用 JQuery 按值检查 XML 节点是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1436413/

相关文章:

jquery - 类型错误 : $container. 同位素不是函数(Wordpress 和同位素)

jquery - 使用 li<a> 和 jquery 突出显示子菜单

javascript - 从父元素到子元素覆盖游标值

javascript - 在 Jquery 中获取 Ajax 返回的值以及 html 标记

c# - 如何检查 XmlDocument C# 中的 XmlDeclaration

Python 摩尔斯电码 : S. O.S

android - Android Studio JavaCv和OpenCv:在 'camera_type'包中找不到属性 'android.javacv'的资源标识符

python - 从 Odoo 联系人 ListView 中选择所有记录 ID

php - 在搜索中使用 mysql 和 php 处理大量数据

c++ - 实现一个接收和处理客户端请求的服务器(cassandra 作为后端),Python 还是 C++?