在td标签下选择next / nearest div

时间:2014-01-28 09:17:43

标签: javascript jquery

我想在div有一些值时隐藏$row[12]类问题

 <td class="status">
     <div class="recstatus">$row[12]</div>
     <div class="question">

     //something

     </div>

    </td>

这是JQuery:

$(document).ready(function () {
    $('.statusselect').each(function () {
       // someting like this it's not working
       var status = $(this).next('.recstatus');
    if ( status.text() == '1' || status.text() == '2' ||  status.text() == '3' ||  status.text() == '4' ) {
       //something like this but it's not working too
       $(this).next('.question').hide();
    }
  });
});

我想使用下一个/最接近的div,因为这个表单处于循环

3 个答案:

答案 0 :(得分:2)

假设您选择的课程类别为status而不是statusselect.recstatus是后代元素,您应该使用.find().children()方法:

$(document).ready(function () {
    var arr = ['1', '2', '3', '4'];
    $('.status').filter(function() { 
        var t = $(this).find('.recstatus').text();
        return $.inArray(t, arr) > -1;  
    }).find('.question').hide();
});

答案 1 :(得分:1)

   solved i take $('.recstatus').each not $('td').each 

 $(document).ready(function () {
        $('.recstatus').each(function () {
         if ( $(this).text() == '1' || $(this).text() == '2' ||  $(this).text() == '3' ||  $(this).text() == '4' ) {
           $(this).next(".question").hide();
        }
      });
    });

答案 2 :(得分:0)

用它来寻找最近的例如:

$('td.status').closest('div')

然后,使用它来获得下一个最接近的:

$('td.status').closest('div').next('div')

请参阅:http://api.jquery.com/closest/