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

テーブルに着くまでに 3 回上がる必要があり、後でエラーを見つける

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

最初の親は th に、2 番目は tr に、3 番目は table に移動し、テーブル内で .error という名前のクラスを持つ要素を見つけます。

于 2012-09-04T13:11:31.457 に答える