2014-03-29 172 views
0

我试图在第一个或第三个TD被点击时检查第三个TD中的复选框,而不是单击第二个TD时复选框。这里是我的代码,我不能让工作:当点击特定的同级元素时,选中复选框

<tr class="item" id="sku999"> 
     <td>Hat</td> 
     <td class="valueButton"></td> 
     <td><input type="checkbox" name="choice" value="10.00"></td> 
</tr> 

$(document).on({ 
    click: function() { 
     $(this).toggleClass('blueChecked'); 
     $(this).prevAll.toggleClass('blueChecked'); 
     $(this).nextAll.toggleClass('blueChecked'); 
     if (event.target.type !== 'checkbox') { 
      $(':checkbox', this.parent()).attr('checked', function() { 
       return !this.checked; 
      }); 
     } 
    } 
}, "tr.item td:not('.valueButton')"); 

更新:我忘了提,我需要能够切换复选框。

UPDATE:已解决!

$(document).on({ 
    click: function() { 
     $("td:not('.valueButton')", $(this).parent()).toggleClass('blueChecked'); 

     if (event.target.type !== 'checkbox') { 
      $(':checkbox', $(this).parent()).attr('checked', function() { 
       return !this.checked; 
      }); 
     } 
    } 
}, "tr.item td:not('.valueButton')"); 
+0

尝试使用'.prop()',而不是'.attr()',你也并不需要回调在这里,你可以简单的写'$(” :'checkbox',this.parent())。prop('checked',!this.checked)' –

+0

感谢,这一半的作品,但它只适用于检查复选框,它不能在第二次点击取消选中:'$(':checkbox',$(this).parent())。prop('checked',!$(this).checked);' – cianz

+0

对不起,我忘了提及我需要能够切换复选框,这是我现在遇到的问题。 – cianz

回答

0

试试这个

$('td:not(:nth-child(1))').click(function{ 
      $('td:nth-child(2) checkbox').prop('checked',true); 
    }); 
相关问题