[英]How to make entire row along with the checkbox in it, click and unclick when I anywhere click inside the table row?
I have a table 我有桌子
<tr>
<td><input name="service_id[]" value="17" type="checkbox"></td>
<td class="right">$175.00</td>
<td>add 0 days to delivery</td>
</tr>
And jQuery 和jQuery
$('#tableSelect tr').click(function() {
if ($(this).find('td input:checkbox').prop('checked') == true)
$(this).find('td input:checkbox').prop('checked', false);
else
$(this).find('td input:checkbox').prop('checked', true);
});
My goal is to have the checkbox click ON and OFF as I click on anywhere in the table row . 我的目标是在单击表行中的任何位置时,使复选框分别单击“打开”和“关闭”。
However what I get is I can click anywhere on the table row to click and unclick, but when I click inside the checkbox itself, it does not register my click. 但是我得到的是我可以单击表格行中的任意位置以单击和取消单击,但是当我在复选框本身内单击时,它不会记录我的单击。
How can I make entire row and the checkbox in the row click and unclick when I click anywhere inside the entire table row, including entire the checkbox itself? 当我单击整个表格行内的任何位置(包括整个复选框本身)时,如何使整个行和该行中的复选框单击并取消单击?
Add a click event to the input
s and then use stopPropagation
to prevent event bubbling. 将click事件添加到input
s,然后使用stopPropagation
防止事件冒泡。
$('#tableSelect tr').click(function() { ele = $(this).find('td input:checkbox')[0]; ele.checked = ! ele.checked; }); $('input:checkbox').click(function(e){ e.stopPropagation(); })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <table id="tableSelect"> <tr> <td> <input name="service_id[]" value="17" type="checkbox"> </td> <td class="right">$175.00</td> <td>add 0 days to delivery</td> </tr> </table>
You need to register change event
also, so bind it like this: 您还需要注册change event
,因此像这样绑定它:
$('#tableSelect tr').on('click change',function() {
if ($(this).find('td input:checkbox').prop('checked') === true)
$(this).find('td input:checkbox').prop('checked', false);
else
$(this).find('td input:checkbox').prop('checked', true);
});
Check the below snippet 检查以下代码段
$('#tableSelect tr').on('click change', function() { if ($(this).find('td input:checkbox').prop('checked') === true) $(this).find('td input:checkbox').prop('checked', false); else $(this).find('td input:checkbox').prop('checked', true); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="tableSelect"> <tr> <td> <input name="service_id[]" value="17" type="checkbox"> </td> <td class="right">$175.00</td> <td>add 0 days to delivery</td> </tr> <tr> <td> <input name="service_id[]" value="17" type="checkbox"> </td> <td class="right">$175.00</td> <td>add 0 days to delivery</td> </tr> <tr> <td> <input name="service_id[]" value="17" type="checkbox"> </td> <td class="right">$175.00</td> <td>add 0 days to delivery</td> </tr> <tr> <td> <input name="service_id[]" value="17" type="checkbox"> </td> <td class="right">$175.00</td> <td>add 0 days to delivery</td> </tr> <tr> <td> <input name="service_id[]" value="17" type="checkbox"> </td> <td class="right">$175.00</td> <td>add 0 days to delivery</td> </tr> <tr> <td> <input name="service_id[]" value="17" type="checkbox"> </td> <td class="right">$175.00</td> <td>add 0 days to delivery</td> </tr> <tr> <td> <input name="service_id[]" value="17" type="checkbox"> </td> <td class="right">$175.00</td> <td>add 0 days to delivery</td> </tr> <tr> <td> <input name="service_id[]" value="17" type="checkbox"> </td> <td class="right">$175.00</td> <td>add 0 days to delivery</td> </tr> </table>
$('#tableSelect tr *').click(function(e) {
var $cb = $(this).parent('tr').find('input[type=checkbox]');
var c = $cb.prop('checked');
if (c === undefined)
e.stopPropagation();
else
$cb.prop('checked', !c);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.