按同级对标记值进行分组
Group tag values by sibling
我试图使用jQuery操纵XML。特别是,我想通过GroupName
分组Question
标签的值,这样我就可以创建一个jQuery手风琴,GroupName
作为标题,GroupName
的问题作为内容。
<?xml version="1.0"?>
<GenericFormData>
<UserQuestions>
<UserQuestion>
<BoxID>Box_1_6</BoxID>
<Question>Refund requested?</Question>
<FieldType>3</FieldType>
<GroupName/>
</UserQuestion>
<UserQuestion>
<BoxID>Box_1_7</BoxID>
<Question>Would you like to receive forms?</Question>
<FieldType>3</FieldType>
<GroupName/>
</UserQuestion>
<UserQuestion>
<BoxID>Box_2_14</BoxID>
<Question>Listing nihil?</Question>
<FieldType>3</FieldType>
<GroupName>VIII</GroupName>
</UserQuestion>
<UserQuestion>
<BoxID>Box_2_15</BoxID>
<Question>Creation date</Question>
<FieldType>2</FieldType>
<GroupName>Date and signature</GroupName>
</UserQuestion>
<UserQuestion>
<BoxID>Box_2_16</BoxID>
<Question>Name of the person signing</Question>
<FieldType>0</FieldType>
<GroupName>Date and signature</GroupName>
</UserQuestion>
<UserQuestion>
<BoxID>Box_2_17</BoxID>
<Question>function of the person signing</Question>
<FieldType>0</FieldType>
<GroupName>Date and signature</GroupName>
</UserQuestion>
<UserQuestion>
<BoxID>Box_2_18</BoxID>
<Question>Phone number of the person signing</Question>
<FieldType>0</FieldType>
<GroupName>Date and signature</GroupName>
</UserQuestion>
<UserQuestion>
<BoxID>Box99</BoxID>
<Question>Liability</Question>
<FieldType>1</FieldType>
<GroupName>VII</GroupName>
</UserQuestion>
</UserQuestions>
</GenericFormData>
jQuery: $(xmlDoc).find('GroupName').filter(function () {
return $(this).text() === groupnames[i].toString();
}).parents().find('UserQuestion').each(function () {
//logic to display `Question` goes here
});
但是它不能正常工作。我迭代groupnames
(包含唯一的值,因为它应该),但each()
方法产生太多。有人知道我哪里做错了吗?感谢。
问题
如果我理解正确的话;我认为你的错误在第三行
}).parents().find('UserQuestion').each(function () { ^----------------------^
当前你选择了所有的parents()
,这基本上将
获取当前匹配集合中每个元素的祖先元素,可由选择器筛选。
这意味着通过选择父元素,您将获得比UserQuestion
更高的元素,这将认为您之前的过滤是多余的。
如果您能够确定groupName
将是一个直接子,那么您可以使用parent()
,省略复数。或者,如果这不能保证,您可以尝试在当前语句中添加选择器,只选择UserQuestion
s,如下所示:
$(xmlDoc).find('GroupName').filter(function () {
return $(this).text() === groupnames[i].toString();
}).parents('UserQuestion').each(function () {
//logic to display `Question` goes here
});
相关文章:
- 没有找到相关文章