2012-01-22 5 views
0
<table> 
    <tr> 
     <td><input type="checkbox"></td><td>aa</td><td>ss</td> 
    </tr> 
    <tr> 
     <td><input type="checkbox"></td><td>aa</td><td>ss</td> 
    </tr> 
    <tr> 
     <td><input type="checkbox"></td><td>aa</td><td>ss</td> 
    </tr> 
</table> 

LIVE:http://jsfiddle.net/6U5WH/TRでチェックしてチェックを外し、チェックボックスのための最善の方法

方法である私は、現在のチェックボックスでTRをクリックすると、チェックしてチェックを外し、チェックボックスのための最善の方法は? HTML(label?)のみで可能ですか?そうでなければjQueryを使いたいと思います。

答えて

6
// when the row is clicked toggle the checked property 
$('tr').click(function() { 
    var $cb = $(this).find(':checkbox'); 
    $cb.prop('checked',!$cb.prop('checked')); 
}); 
// prevent a click on the actual checkbox from bubbling to the row 
$('tr :checkbox').click(function(e) { 
    e.stopPropagation(); 
}); 

更新フィドルは:http://jsfiddle.net/6U5WH/1/

+0

私の例です。 –

0

あなたはjQueryの私たちにしています。

は、このコードを試してみてください。

$('.offer').click(function(){ 
     var $checkbox = $(this).find(':checkbox'); 
     $checkbox.prop('checked', !$checkbox[0].checked); 
}); 
2

みんなそんなに速くここにある最高の答えもhttp://jsfiddle.net/staar2/6HtqB/3/

var selector = 'input[type=checkbox]'; 
$('tr').toggle(function() { 
    $(this).find(selector).attr('checked', 'checked'); 
}, function() { 
    $(this).find(selector).removeAttr('checked'); 
}); 
+0

ありがとう、しかしあなたの例では、チェックボックスのみをチェックしていません –

+1

はい、stopPropagation()を使用する必要があります、@tvanfosson、小さな編集http://jsfiddle.net/staar2/6HtqB/4/からコードを取得することができます –

関連する問題