选择父元素的下一个元素

时间:2013-01-07 11:04:40

标签: jquery html checkbox next

这是我的编码,

<tr class="ar">
   <td>
        <input type="checkbox" class="cbx">
   </td>    
   <td>
        <span class="spc">book</span>
   </td> 
</tr>
<tr class="ar"></tr>

我想做什么,如果点击复选框或跨度(在tr内),我想将一个类切换到第二个tr。此外,如果已选中该复选框,则在单击其中任何一个时我需要取消选中。

5 个答案:

答案 0 :(得分:3)

$('td input[type=checkbox], td span').click(function(){
    $(this).closest('tr')
        .find('input[type=checkbox]')
        .prop("checked", false)
        .end()
        .next()
        .toggleClass('yourclass');
});

答案 1 :(得分:2)

试试这个

$(".cbx2").click(function(){
  $(this).closest("tr.ar").next().toggleClass("toggled_class");
});

$(this).closest("tr.ar")将使用tr类为您提供此对象中最接近的父ar元素,而.next将获得该类型的下一个元素,即您的第二个{{1} }}。

答案 2 :(得分:0)

试试这个....如果你想取消选中复选框......

$('#your_checkbox_id').live('click' , function() {
    if($('#your_checkbox_id').is(':checked')) {
        $('#your_checkbox_id').removeAttr('checked');
        $(this).next().toggleClass('class_name');
    }
});

答案 3 :(得分:0)

$('.cbx, .spc').click(function(e){
    $('.cbx').prop('checked', false); // uncheck everything

    $(this).toggleProp(checked, $(this).prop('checked')) // check/uncheck current
           .closest('.ar').toggleClass('yourClass'); // toggle class on next tr
});

答案 4 :(得分:0)

尝试使用这个:http://jsfiddle.net/kPJJf/

$('td input[type=checkbox], td span').click(function () {
       $(this).parents('tr').next('tr').find('span').toggleClass('spc');
   if($(this).parents('tr').next('tr').find('input[type="checkbox"]').is(':checked')==false){
      $(this).parents('tr').next('tr').find('input[type="checkbox"]').prop('checked', true);
   }else{
      $(this).parents('tr').next('tr').find('input[type="checkbox"]').removeAttr('checked');
   }
});
相关问题