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