문제

I have this xml:

<Checklist>
<ChecklistGroup id="" val="1" seq="" ilid="">
    <name>Group1</name>
    <ChecklistItem id="" val="1" seq="1" pcid="" piid="">
        <name>sb1</name>
    </ChecklistItem>
    <ChecklistItem id="" val="1" seq="2" pcid="" piid="">
        <name>sb2</name>
    </ChecklistItem>
    <ChecklistItem id="" val="1" seq="3" pcid="" piid="">
        <name>sb3</name>
    </ChecklistItem>
</ChecklistGroup>
</Checklist>

There can be a lot of ChecklistGroup nodes. How do I select the ChecklistGroup node based on the text of the next node which is 'name'. I want to select the checklistgroup node where it has a name of 'Group1'

How do i do that in javascript/jquery? Thanks in advance.

도움이 되었습니까?

해결책

You can use something like below where $xml is the jQuery object reference to the xml

var $grp = $xml.find('ChecklistGroup > name').filter(function () {
    return $.trim($(this).text()) == 'Group1'
}).parent()

Note: :contains() was not used since it can return partial matches

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top