gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:59:44 25 4
gpt4 key购买 nike

我正在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com