在jQuery中先检索特定元素,再检索其他元素

在jQuery中先检索特定元素,再检索其他元素,jquery,dom,element,Jquery,Dom,Element,我有这样一个html: <fieldset> <legend>Sondage</legend> <input type="hidden" value="formulaire_test/poll1352736068616/testencode" name="pollpost"> <p class="question">Q1</p> <p class="response"> <input id="R010101

我有这样一个html:

<fieldset>
<legend>Sondage</legend>
<input type="hidden" value="formulaire_test/poll1352736068616/testencode" name="pollpost">
<p class="question">Q1</p>
<p class="response">
<input id="R010101" type="radio" value="A" name="R0101">
<label for="R010101">R11</label>
</p>
<p class="response">
<input id="R010102" type="radio" value="B" name="R0101">
<label for="R010102">r12</label>
</p>
<p class="question">Q2</p>
<p class="response">
<input id="R010201" type="radio" value="A" name="R0102">
<label for="R010201">r2</label>
</p>
<p class="response">
<input id="R010202" type="radio" value="B" name="R0102">
<label for="R010202">r22</label>
</p>
<p class="submit">
<input type="submit" value="Votez">
</p>
</fieldset>

桑达格

Q1

R11

r12

Q2

r2

r22

我想使用jQuery,为每个

检索前面的

例如,如果我有响应“r22”,我想检索Q2,如果我有“r12”,我想检索Q1

谢谢:)

试一试,这将得到包含r22的标签的前一个问题

如果您想动态获取问题,请使用

$('.response').each(function(){
      console.log($(this).prevAll('.question:first'));
});
$('.response').each(function(){
      console.log($(this).prevAll('.question:first'));
});