【问题标题】:Retrieve specific element before an other in jQuery在jQuery中检索其他元素之前的特定元素
【发布时间】:2012-11-01 18:33:28
【问题描述】:

我有一个这样的 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>

我想用 jQuery,为每个 &lt;p class="response"&gt; 检索 &lt;p class="question"&gt; juste 之前。

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

谢谢:)

【问题讨论】:

    标签: jquery dom element


    【解决方案1】:
    $('label:contains("r22")').prevAll('.question:first');
    

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多