2014-03-29 69 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); 
    }); 
相關問題