0

我使用 jquery,我想找到前一个具有“错误”类的 div

我的html代码:

<table class="questions">
    <tr>
        <td class="heading">1
            <div class="error"></div>
        </td>

        <td colspan="4">
            <textarea class="textcontrol required" name='questionT136'>
            </textarea>
        </td>
    </tr>

    <tr>
        <td></td>

        <th><label for="reponse137-3">Très satisfaisant</label></th>
        <th><label for="reponse137-4">Satisfaisant</label></th>
        <th><label for="reponse137-5">Insatisfaisant</label></th>
    </tr>

    <tr class="q">
        <td class="heading">
            2
            <div class="error"></div>
        </td>

        <td class="questionradio"><input class="required" id='reponse137-3'
        name='questionN137' type='radio' value='3'></td>

        <td class="questionradio"><input class="required" id='reponse137-4'
        name='questionN137' type='radio' value='4'></td>
    </tr>
</table>

例如,从reponse137-3 开始,我想将值更改为以前的 class="error"。我已经尝试通过以下方式访问该对象:

$('#reponse137-3').prev(".error")

但它不起作用。我认为因为它没有同一个父母,所以我尝试了:

$('#reponse137-3').prev("tr").next(".error")

我能怎么做 ?

4

2 回答 2

4

假设您的.errordiv 始终在同一表行中:

$('#reponse137-3').closest('tr').find('.error:first');

http://jsfiddle.net/vkfF8/

于 2012-09-04T13:04:43.117 回答
2

你需要上三下才能到达餐桌,后来发现错误

$('#reponse137-3').parent().parent().parent().find('.error');

第一个父母去 th,第二个去 tr 和 thirth 到 table,然后在 table 中找到哪个元素的类名为 .error 你会得到它

于 2012-09-04T13:11:31.457 回答